Android V2 MapView Maps Inside User Fragment (NPE)

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

The documentation clearly states that when someone uses MapView , all relevant lifecycle methods ( onCreate() onResume() , etc.) must be called.

Most of the lifecycle methods in Fragment are similar to Activity , but when I switch between my Fragment , I end up with NPE obfuscation in the onDestroy() or onResume() methods.

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

Has someone done this already? Can you provide a sample MapView code in your Fragment class?

+22
android maps google-maps
Dec 11 '12 at 3:01
source share
3 answers

I managed to include MapView (v2) in my own custom snippet built into the ViewPager. In my case, MapView is included in the fragment layout file. I had to call lifecycle methods in MapView ( onCreate() called onCreateView() from the fragment) and manually MapsInitializer.initialize(context) to avoid a NullPointerException from the BitmapDescriptorFactory class (to get a bitmap for markers). This last trick is strange, and I don’t know why the Map system is not properly initialized without this call, maybe it’s just an error in the current version ...

In my case, I did not have a NullPointerException in onResume() or onDestroy() .

+12
Dec 11 '12 at 17:01
source share

I struggled a bit with the PoPy answer, but in the end I succeeded, and this is what I came up with. This is probably useful for others who are also facing this problem:

 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 declaration to your new root element, in this case com.google.android.gms.maps.MapView ) if there is only one element in your layout, as in this example.

+34
Jan 03 '13 at 13:29
source share

When using a separate MapView following two things are vital

  //at Activity @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); MapsInitializer.initialize(this); mapView.onCreate(savedInstanceState); } //or at Fragment @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { MapsInitializer.initialize(getActivity()); mapView.onCreate(mBundle); } //along with the following @Override protected void onResume() { super.onResume(); if (mapView != null) mapView.onResume(); } @Override protected void onDestroy() { super.onDestroy(); if (mapView != null) mapView.onDestroy(); } @Override public void onLowMemory() { super.onLowMemory(); if (mapView != null) mapView.onLowMemory(); } 
-2
Jun 19 '14 at 7:13
source share



All Articles