MapFragment that throws a NullPointerException on the getMapAsync (this) method

I implemented an action that adds MapFragment at runtime. MapFragment xml contains a static fragment and I'm trying to add it at runtime. I also found that Lollipop has some problems adding a map fragment at runtime. Please check. Problem raised and workaround.

I also gave my codes below,

fragment_map.xml

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".fragment.MapsFragment"> <fragment android:id="@+id/map" android:name="com.google.android.gms.maps.SupportMapFragment" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="appCreators.bloodfinder.activity.MapsActivity"/> <include android:id="@+id/layout" layout="@layout/template_custom_spinner"/> </FrameLayout> 

MapsFragment.java

Implements onMapReadyCallback

 public class MapsFragment extends SupportMapFragment implements OnMapReadyCallback 

In onResume

 @Override public void onResume() { super.onResume(); ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMapAsync(this); } 

it always returns me to zero, and I also tried,

 ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).getMapAsync(this); 

it also returns a NullPointerException

MapsActivity.java

 getSupportFragmentManager().beginTransaction().add(R.id.fragmentContainer, MapsFragment.newInstance()).commit(); 

I add this to the onCreate Activity callback method.

I cannot understand why I am still getting a NullPointerException !

Sometimes I get Attempt to invoke interface method 'void com.google.maps.api.android.lib6.e.fl.o()' on a null object reference

Help will be appreciated!

UPDATE: Still not fixed, I get the following error. I looked into the magazines, but had no idea why this was happening.

 Unable to resume activity {MapsActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'void com.google.maps.api.android.lib6.e.fl.o()' on a null object reference 
+9
android nullpointerexception android-fragments supportmapfragment mapactivity
source share
5 answers

I finally solved this problem for myself by observing these answers in SO. I compared my code and the code in the answers. It seems to me that there is some confusion regarding this API, and the Google implementation is very wide. I have repeatedly received this exception:

 Attempt to invoke interface method 'void com.google.maps.api.android.lib6.e.fl.o()' on a null object reference 

I still don't know what happened to some of these implementations on SO issues that get the same exception, but it worked for me, and it might work for someone else.

This is what you need to check:

  • Make sure you have both of them (I only had api meta data)

      <meta-data android:name="com.google.android.geo.API_KEY" android:value="@string/google_maps_key" /> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> 
  • You have a snippet in xml like this, with the class attribute being correct, as shown below

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

In my case, the map fragment is a child, inside another fragment.

This parent fragment, apparently, should not be MapFragment or SupportMapFragment. After changing it from SupportMapFragment to Fragment, the map showed excellent.

 public class ParentFragment extends Fragment { 

I left everything else as before, but instead of getting the map in onCreateView, I got it in onViewCreated as follows:

 @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.rewards_google_map_area); if (mapFragment != null) { LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE); if (locationManager != null && mapFragment.getMap() != null) { googleMap=mapFragment.getMap(); //after this you do whatever you want with the map } } } 

I did not change anything, and I did not bother with rootView or parentView. As you could see in the answer @Konstantin Loginov

Give it a try and let me know.

+15
source share

I am struggling with a similar problem a while ago. The key was in getMap() at the right time, in onViewCreated() Fragment :

 public class LocationFragment extends Fragment { private SupportMapFragment locationMap; private View rootView; protected View getFragmentView() { View view = getView(); if (view == null) { view = rootView; } return view; } @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View rootView = getFragmentView(); if (rootView != null) { ViewGroup parent = (ViewGroup) rootView.getParent(); if (parent != null) { parent.removeView(rootView); } } try { rootView = inflater.inflate(R.layout.fragment_signup_specify_location, container, false); } catch (InflateException ex) { Log.e(LOG_TAG, "Inflate Exception in onCreateView; Caused by map fragment", ex); } ...... return getFragmentView(); } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); locationMap = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.locationMap); if (locationMap != null) { LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE); if (locationManager != null && locationMap.getMap() != null) { locationMap.getMap().setMyLocationEnabled(true); } } } } 

And the XML for it looks like yours:

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

Then in onViewCreated() you can notify Activity that your map fragment is ready for use.

Hope this helps

+2
source share

In my case, I use raw MapView statically in the XML fragment. The problem was that I had to move the call forwarding mMapView.onCreate(...) to onViewCreated(...) , because in onCreate() mMapView is not overpriced yet. If you are not using MapView , you should double check at what point you call getMap() . I need the internal MapView to be overpriced.

+2
source share

UPDATE 2019

If you still have problems try this:

Do not extend SupportMapFrangemt!

 public class MyFragment extends Fragment { private SupportMapFragment fragment; private GoogleMap map; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.layout_with_map, container, false); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); FragmentManager fm = getChildFragmentManager(); fragment = (SupportMapFragment) fm.findFragmentById(R.id.map_container); if (fragment == null) { fragment = SupportMapFragment.newInstance(); fm.beginTransaction().replace(R.id.map_container, fragment).commit(); } } 

}

+1
source share

If you are using mapView, make sure you mapView.onCreate() before mapView.getMapAsync() . If you use a fragment and the view is still not initialized, you can also call mapView.onCreate() in the onViewCreated() method.

0
source share

All Articles