Android Maps V2 MapView inside custom fragment (NPE)

dnkoutso picture dnkoutso · Dec 11, 2012 · Viewed 23.2k times · Source

I do not want to use or extend SupportMapFragment or MapFragment. I have my own base class with a bunch of code in it.

The documentation clearly states that when someone uses MapView by itself, all corresponding lifecycle methods (onCreate() onResume() etc.) should be called.

Most of the lifecycle methods in a Fragment are similar to an Activity but when I switch back and forth between my Fragment I eventually get an obfuscated NPE in onDestroy() or in onResume() methods.

All the samples provided use an Activity with a MapView but not a custom Fragment.

Has someone done that already? Can you provide sample code of a MapView in your own Fragmentclass?

Answer

Jens Kohl picture Jens Kohl · Jan 3, 2013

I struggled a bit with PoPy's answer but in the end I managed it and here is what I came up with. Probably this is helpful to others which will also come across this issue:

public class MyMapFragment extends Fragment {

    private MapView mMapView;
    private GoogleMap mMap;
    private Bundle mBundle;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View inflatedView = inflater.inflate(R.layout.map_fragment, container, false);

        try {
            MapsInitializer.initialize(getActivity());
        } catch (GooglePlayServicesNotAvailableException e) {
            // TODO handle this situation
        }

        mMapView = (MapView) inflatedView.findViewById(R.id.map);
        mMapView.onCreate(mBundle);
        setUpMapIfNeeded(inflatedView);

        return inflatedView;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBundle = savedInstanceState;
    }

    private void setUpMapIfNeeded(View inflatedView) {
        if (mMap == null) {
            mMap = ((MapView) inflatedView.findViewById(R.id.map)).getMap();
            if (mMap != null) {
                setUpMap();
            }
        }
    }

    private void setUpMap() {
        mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
    }

    @Override
    public void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mMapView.onPause();
    }

    @Override
    public void onDestroy() {
        mMapView.onDestroy();
        super.onDestroy();
    }
}

And here is the corresponding res/layout/map_fragment.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.google.android.gms.maps.MapView
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

You can omit the RelativeLayout (and move the xmlns declartion up to your new root element, in this case the com.google.android.gms.maps.MapView) if you have just one element in your layout like in this example.