google maps are returning null pointer exception

后端 未结 3 1843
孤城傲影
孤城傲影 2021-01-29 02:09

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

相关标签:
3条回答
  • 2021-01-29 02:39
    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

    0 讨论(0)
  • 2021-01-29 02:52

    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.

    0 讨论(0)
  • 2021-01-29 02:56

    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!

    0 讨论(0)
提交回复
热议问题