mapFragment.getMap() returns null
Asked Answered
D

3

6

I'm trying to get a map from a SupportMapFragment but it returns null. From what I read this could be because the fragment is not yet fully displayed and therefore no map exists?! I tried fixing it using executePendingTransactions() but with no success so far.

Any ideas how to fix it?

Here is the code

private GoogleMap map;
private SupportMapFragment mapFragment;
@Override
public void onCreate( Bundle savedInstanceState ) {

    //...
    super.onCreate( savedInstanceState ); 
    setContentView( R.layout.screen_mission2 );
    GoogleMapOptions mapOptions = new GoogleMapOptions();

    mapOptions.mapType(GoogleMap.MAP_TYPE_NORMAL)
        .compassEnabled(true)
        .rotateGesturesEnabled(false)
        .tiltGesturesEnabled(false);

    android.support.v4.app.FragmentManager myFragmentManager = getSupportFragmentManager();
    android.support.v4.app.FragmentManager.enableDebugLogging(true);
    mapFragment = SupportMapFragment.newInstance(mapOptions);
    FragmentTransaction fragmentTransaction = myFragmentManager.beginTransaction();
    fragmentTransaction.add(R.id.mapFragment, mapFragment);
    fragmentTransaction.commit();
    myFragmentManager.executePendingTransactions();

    if(mapFragment == null) Base.log("mapFragment==null");
    if(map==null){
        map = mapFragment.getMap();
        Base.log("map should have been initialized");
        if(map==null) Base.log("map still null");
    }
}

And the layout file:

<fragment
    android:id="@+id/mapFragment"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

It returns the following log

V/FragmentManager(24224): add: SupportMapFragment{4078c4b8 id=0x7f06003d}
V/FragmentManager(24224): Allocated fragment index SupportMapFragment{4078c4b8 #1 id=0x7f06003d}
V/FragmentManager(24224): moveto CREATED: SupportMapFragment{4078c4b8 #1 id=0x7f06003d}
D/EMR     (24224): map should have been initialized
D/EMR     (24224): map still null
Drumstick answered 21/7, 2013 at 10:52 Comment(4)
Here have you declared the mapFragment variable?Slalom
Yes the I declared it(edited the code part, see above)Drumstick
Is it initialized? Otherwise it will be the same as nothing, i.e., null.Slalom
doesn't mapFragment = SupportMapFragment.newInstance(mapOptions); take care of that? At least if(mapFragment == null) Base.log("mapFragment==null"); doesn't appear in the logDrumstick
M
14

Try moving all code that references your GoogleMap to onStart() or onResume(). The map in a map fragment isn't instantiated until after the fragment has gone through onCreateView (link), which happens after the parent activity has gone through onCreate(). Also, you need to check your GoogleMap for null regardless, because if google play services aren't installed, or the map isn't available for some other reason, it will be null.

Mufinella answered 23/12, 2013 at 16:50 Comment(0)
S
1

From what I read this could be because the fragment is not yet fully displayed and therefore no map exists?

Correct.

Any ideas how to fix it?

Actually use the layout file, by calling setContentView(), and get rid of all the FragmentTransaction stuff. You can then retrieve the already-created SupportMapFragment and use it:

setContentView(R.layout.activity_main);

SupportMapFragment mapFrag=(SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.mapFragment);
Strident answered 21/7, 2013 at 11:15 Comment(8)
Thanks, I thought about that but can I set the options specified in GoogleMapOptions in my code then in the xml file or set them later?Drumstick
@cos4: There are equivalents in the XML, though some people have reported issues when using those in Eclipse due to some Eclipse/ADT bug. If that does not work, get rid of the SupportMapFragment in the XML, replacing it with a FrameLayout, stick with your existing FragmentTransaction (pouring the fragment into the FrameLayout), but then you need to delay your work with the GoogleMap for a bit (e.g., subclass SupportMapFragment and put your code in onActivityCreated()).Strident
Is there any doc on setting options for maps in XML? Couldn't find any... Otherwise if I have to subclass SupportMapFragment anyway I could also send an intent from onActivityCreated() and catch it from my current activity?!Drumstick
@cos4: "Is there any doc on setting options for maps in XML?" -- developers.google.com/maps/documentation/android/… "I could also send an intent from onActivityCreated() and catch it from my current activity?" -- I don't see how, or what good that it would do you. If you create your own SupportMapFragment subclass, delegate configuration of the map to the subclass.Strident
Thanks! The idea was: I need to wait until the SupportMapFragment activity is created, so I would modify onActivityCreated() to send me an intent telling me that I can now created a map object since SupportMapFragment is ready.Drumstick
@cos4: Since your fragment is already part of your activity, just call a method on the activity. No need to mess with Intents.Strident
Ah yes, thanks that would be an option. However using the XML options I do get a map (need to use Project->clean to get rid of the Eclipse bug) but I get a white map and Process com.google.android.apps.maps:GoogleLocationService (pid 4604) has died. Dunno if that is related though.Drumstick
@cos4L: Don't know what to tell you about that, sorry.Strident
K
1

You have to implement OnMapReadyCallback, define its public void onMapReady(GoogleMap map) and use it to operate on the fragment as stated in the Google API

Kibbutznik answered 21/11, 2015 at 9:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.