Shao Chen
Shao Chen

Reputation: 31

Null reference exception when using CameraUpdateFactory with MapFragment in an Android FragmentActivity

this is my first post and I would like to do a self Q&A here. :)

When embedding a MapFragment (from Google Maps Android API v2) to an fragment activity, I encountered an internal Google Maps Android API null reference exception with the following steps:

  1. Call CameraUpdateFactory.newLatLngZoom in onMapReady(…)
  2. Leave the activity
  3. Start the activity again

Exception

Process: com.t.y, PID: 21109
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.t.y/com.t.y.activity.SelectLocationActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377)
at android.app.Activity.setContentView(Activity.java:2144)
at com.t.y.activity.SelectLocationActivity.onCreate(SelectLocationActivity.java:113)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
... 10 more
Caused by: java.lang.NullPointerException: null reference
at com.google.android.gms.internal.jx.i(Unknown Source)
at com.google.android.gms.maps.CameraUpdateFactory.a(Unknown Source)
at com.google.android.gms.maps.MapsInitializer.a(Unknown Source)
at com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source)
at com.google.android.gms.maps.MapFragment$b.nO(Unknown Source)
at com.google.android.gms.maps.MapFragment$b.a(Unknown Source)
at com.google.android.gms.dynamic.a.a(Unknown Source)
at com.google.android.gms.dynamic.a.onInflate(Unknown Source)
at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source)
at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2115)
at android.app.Activity.onCreateView(Activity.java:5282)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
... 20 more

Code

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_select_location);
    …
    MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap map) {
    mMap = map;
    …
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), zoom));
}

XML layout

<?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" >

<fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.MapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<ImageView
    android:id="@+id/imgCenter"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:contentDescription="Center"
    android:src="@drawable/chat_input_01" />

<Button
    android:id="@+id/btnShareLocation"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/imgCenter"
    android:layout_centerHorizontal="true"
    android:text="@string/share_location" />

</RelativeLayout>

After some studies, I found this https://developer.android.com/reference/com/google/android/gms/maps/GoogleMapOptions.html#useViewLifecycleInFragment(boolean)

I try to set the attribute "useViewLifecycle" to "true" in the layout file and that solves my problem:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.MapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    map:useViewLifecycle="true" />

Upvotes: 3

Views: 282

Answers (1)

Pavel Dudka
Pavel Dudka

Reputation: 20944

I had exactly the same problem. What I discovered - is that the cause for that was enabled StrictMode (particularly detectDiskReads rule) within the activity which hosts this map fragment.

Also I noticed is that it crashes only when MapFragment is used as a nested fragment. When you move SupportMapFragment to the root activity layout - it works fine.

When StrictMode is disabled - it works fine as well.

Created a bug for that - https://code.google.com/p/android/issues/detail?id=159493

Upvotes: 1

Related Questions