Android Studio:膨胀 class 片段时出错

Android Studio : Error inflating class fragment

我正在编写一个包含 google 地图片段的 android 应用程序。当我从 activity 中排除地图片段时,应用程序运行正常。当我包含地图片段时,我得到了下面块引用中引用的异常。最令人恼火的是,就在几周前,这还没有发生。这个 activity 运行 几周都很好,然后我更新了 android studio,现在我遇到了这个异常。我在 google 和 Whosebug 上的几个线程中搜索了解决方案,但没有任何效果。我什至尝试直接复制并粘贴地图 activity 的 google android 演示,但我得到了同样的异常。请帮忙。

引发的异常(从 android 监视器复制):

FATAL EXCEPTION: main Process: com.androidapps.seanmcalinden.firstandroidapp, PID: 23089 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidapps.seanmcalinden.firstandroidapp/com.androidapps.seanmcalinden.firstandroidapp.MapsActivity}: android.view.InflateException: Binary XML file line #55: Binary XML file line #55: 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 #55: Binary XML file line #55: 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 com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393) at android.app.Activity.setContentView(Activity.java:2166) at com.androidapps.seanmcalinden.firstandroidapp.MapsActivity.onCreate(MapsActivity.java:67) 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 #55: Error inflating class fragment at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) at android.view.LayoutInflater.inflate(LayoutInflater.java:515) at android.view.LayoutInflater.inflate(LayoutInflater.java:423)  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:2166)  at com.androidapps.seanmcalinden.firstandroidapp.MapsActivity.onCreate(MapsActivity.java:67)  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.y.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.MapFragment$zza.onCreateView(Unknown Source) at com.google.android.gms.dynamic.zza.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.MapFragment.onCreateView(Unknown Source) at android.app.Fragment.performCreateView(Fragment.java:2220) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:949) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126) at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1228) at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2201) at android.app.FragmentController.onCreateView(FragmentController.java:98) at android.app.Activity.onCreateView(Activity.java:5546) at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:34) at android.support.v4.app.FragmentActivity.onCreat

OnCreate 方法:

    protected void onCreate(Bundle savedInstanceState) {
    Log.e("success", "entered onCreate in MapsActivity");

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);

    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    MapFragment mapFragment =
            (MapFragment) getFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    if (locationGAC == null) {
        // ATTENTION: This "addApi(AppIndex.API)"was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        locationGAC = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .addApi(AppIndex.API).build();
        Log.e("success", "created locationGAC");
    }

    Log.d("myTag", "test output 1");

    createFriendList();

}

此 activity 的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="@color/colorPrimary"
android:weightSum="1" >

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" >

    <Button
        android:layout_width="70dp"
        android:layout_height="60dp"
        android:text="Back"
        android:id="@+id/button"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignBottom="@+id/SearchBar1"
        android:onClick="backToLogin"
        android:background="@color/colorPrimaryDark"
        android:textColor="#ffffff" />

    <EditText
        android:layout_width="0dp"
        android:layout_height="60dp"
        android:id="@+id/SearchBar1"
        android:layout_alignParentTop="true"
        android:hint="Search"
        android:layout_alignParentEnd="false"
        android:layout_alignParentStart="false"
        android:layout_toEndOf="@id/button"
        android:layout_toStartOf="@+id/SearchButton"
        android:textColor="#ffffff"
        android:textColorHint="#ffffff" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="70dp"
        android:layout_height="60dp"
        android:text="Go"
        android:id="@+id/SearchButton"
        android:layout_alignParentRight="true"
        android:enabled="true"
        android:onClick="onSearch"
        android:textColor="#ffffff"
        android:background="@color/colorPrimaryDark" />

</RelativeLayout>

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


<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="60dp"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal">

    <Button
        android:layout_width="70dp"
        android:layout_height="60dp"
        android:text="Retrieve"
        android:id="@+id/serverRetrieveButton"
        android:layout_gravity="left"
        android:textColor="#ffffff"
        android:onClick="getLocationFromDexelus"
        android:background="@color/colorPrimaryDark" />

    <Button
        android:layout_width="70dp"
        android:layout_height="60dp"
        android:text="Send"
        android:id="@+id/serverSendButton"
        android:layout_alignParentRight="true"
        android:textColor="#ffffff"
        android:onClick="sendLocationToDexelus"
        android:background="@color/colorPrimaryDark" />

</RelativeLayout>

</RelativeLayout>

如上所述,我已经尝试了在 Whosebug 上找到的许多解决方案。并不是说您不应该再 post 那些,而是我认为我的问题可能比那些更复杂或具体,因为 none 给定的解决方案对我有用。

谢谢。

我认为错误来自行 class="com.google.android.gms.maps.MapFragment" 可能路径不对

出于某种原因,当我将我的 sdk 更新到周三发布的版本时,我的问题消失了。我想这是合适的,因为问题是在 android 工作室更新后首次出现的。感谢您的建议!