Android GoogleMap or SupportMapFragment - null pointer exception

前端 未结 8 2010
心在旅途
心在旅途 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

    After hours of search, I did in this way:

     public class MapFragment extends Fragment {
        private GoogleMap map;
        SupportMapFragment mapFrag;
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            setHasOptionsMenu(true);
            View rootView = inflater.inflate(R.layout.fragment_map, container,
                    false);
    
            fm = getActivity().getSupportFragmentManager();
            map = ((SupportMapFragment) fm.findFragmentById(R.id.map)).getMap();
    
            return rootView;
    
        }
    }
    

提交回复
热议问题