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 f
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.GoogleMap com.google.android.gms.maps.SupportMapFragment.getMap()' on a null object reference
getChildFragmentManager()
instead of getActivity().getSupportFragmentManager()
initilizeMap();
from your onResume method .Reference
SupportMapFragment.getMap() on a null object reference
You have to wait until map getting loaded....
public class ChatFragment extends Fragment implements OnMapReadyCallback {
private void initilizeMap() {
//SupportMapFragment mapFragment = (SupportMapFragment) fragmentManager.findFragmentById(R.id.map);
SupportMapFragment mapFrag = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
mapFrag.getMapAsync(this);
}
@Override
public void onMapReady(final GoogleMap map) {
googleMap = map;
//Do other stuff..........
}
}
Edit
SupportMapFragment mapFrag = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
Check the edit code, use ChildFragmentManager.
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!