Android GoogleMap or SupportMapFragment - null pointer exception

前端 未结 8 2003
心在旅途
心在旅途 2021-02-10 10:51

In my app i showing google map version2 in a fragment. but i get Null pointer exception at

mMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id

8条回答
  •  [愿得一人]
    2021-02-10 11:07

    None of these solutions fixed it for me -- but this answer from Mark Murphy (https://code.google.com/p/android/issues/detail?id=77714) did:

    Use getChildFragmentManager() to access a nested fragment, not getFragmentManager(). http://developer.android.com/reference/android/support/v4/app/Fragment.html#getChildFragmentManager%28%29

    i.e.:

    public class MapFragment extends Fragment {
        private GoogleMap map;
        private SupportMapFragment mapFragment;
        ...
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup vg, Bundle data) {
            View view = inflater.inflate(R.layout.map_view, vg, false);
            FragmentManager fm = getChildFragmentManager();
            mapFragment = (SupportMapFragment) fm.findFragmentById(R.id.map);
            mapFragment.getMapAsync(new OnMapReadyCallback() {
                public void onMapReady(GoogleMap googleMap) {
                    map = googleMap;
                    initMap();
                }
            });
        }
    }
    

提交回复
热议问题