Crash when the visibility of MapView is set to GONE
User reported crash, occurs when you set the visibility to GONE in xml.
I did a test with our Press for Marker Activity where I set the visibility to View.GONE in xml and hooked into the reset button click event to set visibility to View.VISIBLE.
Following gif shows no crash and everything is working as expected:

Closing as not reproducible..
@tobrun
@zhangkaiyue0701 Thank you very much for sharing your code, this was very helpful to troubleshoot your issue.
I was able to reproduce your crash. It seems the problem is that the map view is started as View.GONE (from the XML) but it's never set back to View.VISIBLE in the activity. I've refactored your setOnClickListener to show you one way to fix this:
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mapView.getVisibility() == View.GONE) {
Log.d("onClick", "Map is gone, making it visible.");
mapView.setVisibility(View.VISIBLE);
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(MapboxMap mapboxMap) {
Log.d("onMapReady", "Map is ready.");
map = mapboxMap;
switchStyle();
}
});
} else {
switchStyle();
}
}
});
And switchStyle() is simply:
public void switchStyle() {
if (huan) {
map.setStyleUrl("mapbox://styles/yueyingky/cirx0wx4c002kgum64as7gc67");
} else {
map.setStyleUrl("mapbox://styles/yueyingky/cirx94p230003g3ncicn3vijl");
}
huan = !huan;
}
They key part is to invoke mapView.setVisibility(View.VISIBLE); to make the map visible and getMapAsync() to make sure map isn't null before trying to switch the styles. This is the result:

@zugaldia thank you!!!
@zugaldia
@zhangkaiyue0701 could you send the updated logcat for this crash?
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.zhangkaiyue.mapboxdemo/com.example.zhangkaiyue.mapboxdemo.MainActivity}: android.view.InflateException: Binary XML file line #10: Error inflating class com.mapbox.mapboxsdk.maps.MapView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362)
at android.app.ActivityThread.access$700(ActivityThread.java:168)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1329)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:177)
at android.app.ActivityThread.main(ActivityThread.java:5493)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1225)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1041)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class com.mapbox.mapboxsdk.maps.MapView
at android.view.LayoutInflater.createView(LayoutInflater.java:669)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:745)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:804)
at android.view.LayoutInflater.inflate(LayoutInflater.java:526)
at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method)
at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:631)
at android.view.LayoutInflater.inflate(Native Method)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at android.view.LayoutInflater.inflate(LayoutInflater.java:382)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.zhangkaiyue.mapboxdemo.MainActivity.onCreate(MainActivity.java:40)
at android.app.Activity.performCreate(Activity.java:5372)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362)
at android.app.ActivityThread.access$700(ActivityThread.java:168)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1329)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:177)
at android.app.ActivityThread.main(ActivityThread.java:5493)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1225)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1041)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
at android.view.LayoutInflater.createView(LayoutInflater.java:643)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:745)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:804)
at android.view.LayoutInflater.inflate(LayoutInflater.java:526)
at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method)
at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:631)
at android.view.LayoutInflater.inflate(Native Method)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at android.view.LayoutInflater.inflate(LayoutInflater.java:382)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.zhangkaiyue.mapboxdemo.MainActivity.onCreate(MainActivity.java:40)
at android.app.Activity.performCreate(Activity.java:5372)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362)
at android.app.ActivityThread.access$700(ActivityThread.java:168)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1329)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:177)
at android.app.ActivityThread.main(ActivityThread.java:5493)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1225)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1041)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.mapbox.mapboxsdk.maps.MapView.onVisibilityChanged(MapView.java:1502)
at android.view.View.dispatchVisibilityChanged(View.java:8115)
at android.view.ViewGroup.dispatchVisibilityChanged(ViewGroup.java:1096)
at android.view.View.setFlags(View.java:9488)
at android.view.View.<init>(View.java:3943)
at android.view.ViewGroup.<init>(ViewGroup.java:475)
at android.widget.FrameLayout.<init>(FrameLayout.java:101)
at android.widget.FrameLayout.<init>(FrameLayout.java:97)
at com.mapbox.mapboxsdk.maps.MapView.<init>(MapView.java:171)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
at android.view.LayoutInflater.createView(LayoutInflater.java:643)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:745)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:804)
at android.view.LayoutInflater.inflate(LayoutInflater.java:526)
at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method)
at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:631)
at android.view.LayoutInflater.inflate(Native Method)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at android.view.LayoutInflater.inflate(LayoutInflater.java:382)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.zhangkaiyue.mapboxdemo.MainActivity.onCreate(MainActivity.java:40)
at android.app.Activity.performCreate(Activity.java:5372)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362)
at android.app.ActivityThread.access$700(ActivityThread.java:168)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1329)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:177)
at android.app.ActivityThread.main(ActivityThread.java:5493)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1225)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1041)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)
@zhangkaiyue0701 Thanks for the info, unfortunately I'm unable to reproduce this one. Could you try:
- With the latest
beta2(I see in https://github.com/mapbox/mapbox-gl-native/issues/6287#issuecomment-245570869 that you were usingbeta1). - In a different device or emulator?
- Moving the
MapViewfrom being a direct child ofCoordinatorLayout? For example, to use aRelativeLayoutyou could changeactivity_main.xmlto be like:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
...
<include layout="@layout/content_main" />
<android.support.design.widget.FloatingActionButton
...
/>
</android.support.design.widget.CoordinatorLayout>
And then content_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:showIn="@layout/activity_main">
(Map and button go here)
</RelativeLayout>
Thanks, let me know if this helps.
@zugaldia
1.I used beta2, but it still crashes
2.Another Samsung, SM-G3819D, Android 4.1.2, it also crashed. This phones don't have Google Service, I don't know whether it has related to Google Service.
3.I try RelativeLayout, but it doesn't work.
Tracking this one here now: https://github.com/mapbox/mapbox-gl-native/issues/6317.