Skip to content Skip to sidebar Skip to footer

Google Maps Are Returning Null Pointer Exception

when I am trying to show the maps on my fragment it is returning null pointer exception. I have added everything to the manifest each and every permission. I am attaching the frag

Solution 1:

You have to wait until map getting loaded....

publicclassChatFragmentextendsFragmentimplementsOnMapReadyCallback {

    privatevoidinitilizeMap() {
      //SupportMapFragment mapFragment = (SupportMapFragment) fragmentManager.findFragmentById(R.id.map);SupportMapFragmentmapFrag= (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
      mapFrag.getMapAsync(this);
    }

    @OverridepublicvoidonMapReady(final GoogleMap map) {
        googleMap = map;
        //Do other stuff..........
    }

}

Edit

SupportMapFragment mapFrag = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);

Check the edit code, use ChildFragmentManager.

Solution 2:

Replace this code

SupportMapFragment mapFrag = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
googleMap = mapFrag.getMap();

with

googleMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();

Check it!

Solution 3:

java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.GoogleMap com.google.android.gms.maps.SupportMapFragment.getMap()'on a nullobject reference
  1. You can use getChildFragmentManager() instead of getActivity().getSupportFragmentManager()
  2. Remove initilizeMap(); from your onResume method .

Reference

SupportMapFragment.getMap() on a null object reference

Post a Comment for "Google Maps Are Returning Null Pointer Exception"