google maps are returning null pointer exception

点点圈 提交于 2019-12-02 12:06:01

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!

IntelliJ Amiya
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
  1. You can use getChildFragmentManager() instead of getActivity().getSupportFragmentManager()
  2. Remove initilizeMap(); from your onResume method .

Reference

SupportMapFragment.getMap() on a null object reference

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