Error inflating class fragment with SupportMapFragment

假装没事ソ 提交于 2019-12-12 09:23:53

问题


Here is yet another “Error inflating class fragment with SupportMapFragment” issue.

Here is my fragment XML:

<fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    class="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.myapp.maptest.MapsActivity" />

Here is my activity (it is started from a NavigationDrawer in the main activity):

public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback, GoogleMap.OnMapLongClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

Here are the relevant parts of my Manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
    android:name="com.myapp.maptest.AppControl"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/MyTheme"
    android:allowBackup="false">
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="XXX"/>
    <uses-library android:name="com.google.android.maps" />

obviously there is a real key in there not XXX

here is some relevant code form the build.gradle:

defaultConfig {
    applicationId "com.myapp.maptest"
    minSdkVersion 16
    targetSdkVersion 23
    versionCode 7
    versionName "1.5"
}
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:support-v4:23.1.1'
    compile 'com.android.support:design:23.1.1'
    compile 'com.google.android.gms:play-services:7.5.0'
}

and here is the error Log (Sorry for the messed up format. Copy and Paste):

01-16 22:49:05.431 10672-10672/com.myapp.maptest E/AndroidRuntime: FATAL EXCEPTION: main
                                                                 Process: com.myapp.maptest, PID: 10672
                                                                 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.maptest/com.myapp.maptest.MapsActivity}: android.view.InflateException: Binary XML file line #1: Binary XML file line #1: Error inflating class fragment
                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                     at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                     at android.os.Looper.loop(Looper.java:148)
                                                                     at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                  Caused by: android.view.InflateException: Binary XML file line #1: Binary XML file line #1: Error inflating class fragment
                                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
                                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
                                                                     at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
                                                                     at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
                                                                     at com.myapp.maptest.MapsActivity.onCreate(MapsActivity.java:35)
                                                                     at android.app.Activity.performCreate(Activity.java:6237)
                                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                     at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                     at android.os.Looper.loop(Looper.java:148) 
                                                                     at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                                  Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class fragment
                                                                     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782)
                                                                     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
                                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
                                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
                                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
                                                                     at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256) 
                                                                     at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109) 
                                                                     at com.myapp.maptest.MapsActivity.onCreate(MapsActivity.java:35) 
                                                                     at android.app.Activity.performCreate(Activity.java:6237) 
                                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                     at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                     at android.os.Looper.loop(Looper.java:148) 
                                                                     at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                                  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.io.File.mkdir()' on a null object reference
                                                                     at com.google.maps.api.android.lib6.gmm6.m.ad.a(Unknown Source)
                                                                     at com.google.maps.api.android.lib6.gmm6.c.h.a(Unknown Source)
                                                                     at com.google.maps.api.android.lib6.gmm6.c.ao.a(Unknown Source)
                                                                     at com.google.maps.api.android.lib6.e.bd.a(Unknown Source)
                                                                     at com.google.maps.api.android.lib6.e.ev.a(Unknown Source)
                                                                     at com.google.maps.api.android.lib6.e.z.a(Unknown Source)
                                                                     at com.google.maps.api.android.lib6.e.y.a(Unknown Source)
                                                                     at com.google.android.gms.maps.internal.u.onTransact(SourceFile:107)
                                                                     at android.os.Binder.transact(Binder.java:387)
                                                                     at com.google.android.gms.maps.internal.IMapFragmentDelegate$zza$zza.onCreateView(Unknown Source)
                                                                     at com.google.android.gms.maps.SupportMapFragment$zza.onCreateView(Unknown Source)
                                                                     at com.google.android.gms.dynamic.zza$4.zzb(Unknown Source)
                                                                     at com.google.android.gms.dynamic.zza.zza(Unknown Source)
                                                                     at com.google.android.gms.dynamic.zza.onCreateView(Unknown Source)
                                                                     at com.google.android.gms.maps.SupportMapFragment.onCreateView(Unknown Source)
                                                                     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
                                                                     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1036)
                                                                     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1226)
                                                                     at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1328)
                                                                     at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2284)
                                                                     at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:111)
                                                                     at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:314)
                                                                     at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:31)
                                                                     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:79)
                                                                    at android.view.LayoutInflater.createViewFr

Other stuff from the log:

01-16 22:49:05.381 10672-10672/com.myapp.maptest I/Google Maps Android API: Google Play services client version: 7571000
01-16 22:49:05.391 10672-10672/com.myapp.maptest I/Google Maps Android API: Google Play services package version: 8489480

The thing is, this has worked perfectly fine (even without the gms.version meta-data) until last nights update for Android Studio. At first I thought it was just the emulator image that got messed up, but the app also crashes on my phone as soon as the MapActivity tries to inflate the fragment. Debugging says in crashes at setContentView(R.layout.activity_maps); And I repeat, this exact code has worked before

I have tried to change the activity from AppCompatActivity to FragmentActivity, same error. I have tried to put the fragment XML into a layout, same error. And pretty much anything I found online. Same error.

Any help is greatly appreciated.


回答1:


Sooo, according to this link:

code.google.com/Issue 9021

It is not an error in my code. I'm also getting the

null pointer exception boolean java.io.File.mkdir()

It appears to be a bug in Googles map/play services API or the com.google.android.gms:play-services library. I guess I have to wait for a fix.




回答2:


Bug has been resolved!

https://code.google.com/p/gmaps-api-issues/issues/detail?id=9021&q=NullPointerException&sort=-id&colspec=ID%20Type%20Status%20Introduced%20Fixed%20Summary%20Stars%20ApiType%20Internal

Status = Fixed! :D

Found some info about this in the first answer to this question:

NullPointerException from Google maps



来源:https://stackoverflow.com/questions/34835890/error-inflating-class-fragment-with-supportmapfragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!