I am trying to develop an android app with a google map v2, location service and some control buttons.
I don't want to put all these thing inside one MainActivity class so I thought I could split all the code into some more classes. The MainActivity shall controll all the GUI things and react on map or location events.
Now I have the following problem. Inside my onCreate I instanziate the additional classes:
// Preferences as singleton
pref = Prefs.getInstance(this.getApplicationContext());
pref.loadSettings();
// Set up the location
loc = new Locations(pref);
loc.setCallback(this);
map = new MyMap(pref);
It seems to work fine. But inside the MyMap class every time I start the app a null pointer exception is thrown. I don't know why and how to prevent it. When calling MyMap() the following code will be executed:
[...]
private Prefs pref;
private GoogleMap mMap;
[...]
public MyMap(Prefs prefs) {
pref = (Prefs) prefs;
if (mMap == null) {
FragmentManager fmanager = getSupportFragmentManager();
mMap = ((SupportMapFragment) fmanager.findFragmentById(R.id.map)).getMap();
[...]
}
The line with the findFragmentById is the one that causes the exception. I think it is caused because the fragment manager cannot access the layout loaded inside my MainActivity. But how can I access the fragments and view elements defined within my MainActivity?
Every class has the following head:
public class XYZ extends android.support.v4.app.FragmentActivity implements XXX, YYY
I tried to save the application context within my Prefs() class. But I don't know how to use it inside my additional classes. :(
Can someone help me please?
Thank you very much!!!
Thorsten