Map fragment V2 in a Fragment

后端 未结 4 500
粉色の甜心
粉色の甜心 2021-01-14 13:48

I am facing some issue\"Null pointer Expcepttion at getmap()\" in Google Map Fragments . I want to insert mapfragment in a fragment Please review the code and let know whet

相关标签:
4条回答
  • 2021-01-14 14:21

    Remove all the fmap code:

    SupportMapFragment fmap = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);
    
    if (fmap == null) {
        fmap = SupportMapFragment.newInstance();
        ft.add(R.id.map, fmap);
    }
    

    You already have an instance of the map saved as the "map" variable, so use that one.

    0 讨论(0)
  • gMap = ((MapFragment) getActivity().getFragmentManager().findFragmentById(R.id.map)).getMap();
    
    0 讨论(0)
  • 2021-01-14 14:32

    You need to inflate the layout first. The problem is you're doing that at the end of onCreateView, so the map fragment isn't there when you call findFragmentById.

    Also, PerxDetailedFragment should extend Fragment, not SupportMapFragment.

    public class PerxDetailedFragment extends Fragment {
        // ...
    
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
    
            View v = inflater.inflate(R.layout.perx_detailed, null, false);
    
            map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map))
                .getMap();
    
            //...
    
            return v;
        }
    
    0 讨论(0)
  • 2021-01-14 14:35

    You need to extend SupportMapFragment

    public class MapFragment extends SupportMapFragment{
    
    GoogleMap mGoogleMap;
    Context mContext;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        mContext=getActivity();
        View rootView = inflater.inflate(R.layout.fragment_home, container, false);
        initializeMap(rootView);
        return rootView;
    }
    void initializeMap(View rootView)
    {
        if (mGoogleMap == null) {
            mGoogleMap=((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map4)).getMap();
            // Getting Map for the SupportMapFragment
            mGoogleMap.setMyLocationEnabled(true);
                if (mGoogleMap == null) {
                Toast.makeText(getActivity().getApplicationContext(),"Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            } else {
                setMapDefualtProp();
    
            }
        }else
            setMapDefualtProp();
    }
    
    private void setMapDefualtProp() {
        mGoogleMap.setMyLocationEnabled(true);
        mGoogleMap.setTrafficEnabled(true);
        mGoogleMap.getUiSettings().setMyLocationButtonEnabled(true);
        mGoogleMap.getUiSettings().setRotateGesturesEnabled(true);
        mGoogleMap.getUiSettings().setCompassEnabled(true);
        mGoogleMap.getUiSettings().setZoomGesturesEnabled(true);
        mGoogleMap.getUiSettings().setZoomControlsEnabled(true);        
    } 
    

    }

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