使用SupportMapFragment时,“应用更改”按钮不起作用

时间:2018-10-31 08:00:13

标签: android

如果我有SupportMapFragment,则“应用更改”按钮(黄色)无法成功运行(应用程序会因Error inflating class fragment错误而崩溃)

我必须始终按绿色按钮才能看到任何更改

enter image description here

我有12行

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

错误:

Process: com.example.test, PID: 4940
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}: android.view.InflateException: Binary XML file line #12: Binary XML file line #12: Error inflating class fragment
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2485)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2545)
        at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4189)
        at android.app.ActivityThread.access$1200(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1404)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:157)
        at android.app.ActivityThread.main(ActivityThread.java:5602)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
     Caused by: android.view.InflateException: Binary XML file line #12: Binary XML file line #12: Error inflating class fragment
        at android.view.LayoutInflater.inflate(LayoutInflater.java:543)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
        at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393)
        at android.app.Activity.setContentView(Activity.java:2233)
        at com.example.test.MainActivity.onCreate(MainActivity.java:67)
        at android.app.Activity.performCreate(Activity.java:6358)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2438)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2545) 
        at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4189) 
        at android.app.ActivityThread.access$1200(ActivityThread.java:151) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1404) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:157) 
        at android.app.ActivityThread.main(ActivityThread.java:5602) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652) 

p.s。使用了'com.google.android.gms:play-services-maps:16.0.0'

1 个答案:

答案 0 :(得分:0)

您称之为黄色按钮的按钮是用于即时运行的,之所以造成崩溃,是因为不会将MapFragment添加到BackStack中,因此当即时运行该应用程序时,它将找不到该实例的实例。您可以通过以下方式修复它:

   if (savedInstanceState != null) {
            mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentByTag("map");
        } else {
            mapFragment = SupportMapFragment.newInstance();
            FragmentTransaction mapTransaction = getSupportFragmentManager().beginTransaction();
            mapTransaction.addToBackStack("map").add(R.id.map, mapFragment, "map").commit();
        }
        mapFragment.getMapAsync(this);