MapFragment.getMap () returns null

I am trying to get a map from SupportMapFragment but it returns null. From what I read, this may be due to the fact that the fragment is not yet fully displayed, and therefore the map does not exist ?! I tried to fix it with executePendingTransactions (), but have not yet succeeded.

Any ideas how to fix this?

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 next 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 
+6
source share
3 answers

Try moving all the code that references your GoogleMap to onStart() or onResume() . The map in the map fragment is not created until the fragment passes through onCreateView ( link ), which occurs after the parent activity has passed through onCreate() . In addition, you need to check your GoogleMap for null no matter what, if Google Play services are not installed or the card is unavailable for any other reason, it will be null .

+14
source

From what I read, this may be due to the fact that the fragment is not yet fully displayed, and therefore the map does not exist?

Right.

Any ideas how to fix this?

Actually use the layout file by calling setContentView() and getting rid of all the FragmentTransaction stuff. Then you can get the already created SupportMapFragment and use it:

 setContentView(R.layout.activity_main); SupportMapFragment mapFrag=(SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.mapFragment); 
+1
source

You must implement OnMapReadyCallback , define its public void onMapReady(GoogleMap map) and use it to work with the fragment, as specified in the Google API

+1
source

Source: https://habr.com/ru/post/949926/


All Articles