mapFragment.getMap() returns null

人走茶凉 提交于 2019-11-28 08:13:45

问题


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

回答1:


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.




回答2:


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);



回答3:


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



来源:https://stackoverflow.com/questions/17771545/mapfragment-getmap-returns-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!