Android导航抽屉中的MapFragment导致android.view.InflateException

时间:2016-03-06 09:27:38

标签: android android-fragments navigation-drawer

我正在尝试使用抽屉和地图片段。以下代码适用于API 19,但在模拟器中的API 22和23上抛出android.view.InflateException: Binary XML file line #25: Error inflating class fragment。该代码适用于物理设备(在API 19和API 23上测试)。该错误指向onCreate中的setContentView(R.layout.activity_map);

我已经看到了关于当前错误的类似问题(post),但我不相信这是我的问题,因为这似乎只会影响模拟器设备。

堆栈跟踪:

 Process: com.org.app, PID: 20569
                 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.org.app/com.org.app.MapActivity}: android.view.InflateException: Binary XML file line #25: Error inflating class fragment
                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
                     at android.app.ActivityThread.access$800(ActivityThread.java:151)
                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                     at android.os.Handler.dispatchMessage(Handler.java:102)
                     at android.os.Looper.loop(Looper.java:135)
                     at android.app.ActivityThread.main(ActivityThread.java:5254)
                     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:903)
                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                  Caused by: android.view.InflateException: Binary XML file line #25: Error inflating class fragment
                     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
                     at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
                     at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
                     at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
                     at android.view.LayoutInflater.parseInclude(LayoutInflater.java:916)
                     at android.view.LayoutInflater.rInflate(LayoutInflater.java:802)
                     at android.view.LayoutInflater.parseInclude(LayoutInflater.java:916)
                     at android.view.LayoutInflater.rInflate(LayoutInflater.java:802)
                     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 android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:267)
                     at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:129)
                     at com.org.app.MapActivity.onCreate(MapActivity.java:60)
                     at android.app.Activity.performCreate(Activity.java:5990)
                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
                     at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                     at android.os.Looper.loop(Looper.java:135) 
                     at android.app.ActivityThread.main(ActivityThread.java:5254) 
                     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:903) 
                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
                  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:380)
                     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:1974)
                     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1036)
                     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1230)
                     at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1332)
                     at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2288)
                     at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
                  at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(Fr

activity_map.xml

 <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_map"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_map"
        app:menu="@menu/activity_map_drawer" />

</android.support.v4.widget.DrawerLayout>

content_map.xml

<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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.org.app.MapActivity"
    tools:showIn="@layout/app_bar_map">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/db1_root"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >


        <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="1">

            <!-- MAP VIEW -->
            <fragment xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:id="@+id/map"
                android:name="com.google.android.gms.maps.SupportMapFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context="com.org.app.MapActivity" />

            <!-- + OVERLAY -->
            <TextView
                android:id="@+id/crosshair"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="+"
                android:textColor="#ffffff"
                android:layout_gravity="center"
                android:textSize="60dip"
                android:textStyle="normal"
                android:typeface="sans" />

            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:id="@+id/db2_root"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/areaTextView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Area: "
                    android:textColor="#ffffff"
                    android:textSize="15dip"
                    android:background="#8000"
                    android:textStyle="normal"
                    android:typeface="sans" />
                <TextView
                    android:id="@+id/perimeterTextView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Perimeter: "
                    android:textColor="#ffffff"
                    android:textSize="15dip"
                    android:background="#8000"
                    android:textStyle="normal"
                    android:typeface="sans" />
            </LinearLayout>
        </FrameLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:background="@android:drawable/bottom_bar"
            android:paddingLeft="2.0dip"
            android:paddingTop="1.0dip"
            android:paddingRight="2.0dip"
            android:paddingBottom="1.0dip"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:backgroundTint="#ffffff"
            android:gravity="center|bottom"
            android:layout_weight="0">

            <Button android:id="@+id/clearPinButton"
                android:layout_width="0.0dip" android:layout_height="fill_parent"
                android:layout_weight="1.0"
                android:text="Clear"
                android:onClick="clearLastPin"
                android:backgroundTint="#ff6666"
                android:background="#ff6666"
                android:textColor="#ffffff" />
            <Button android:id="@+id/dropPinButton"
                android:layout_width="0.0dip" android:layout_height="fill_parent"
                android:layout_weight="1.0"
                android:text="Drop Pin"
                android:backgroundTint="#69d2e7"
                android:onClick="dropPin"
                android:background="#69d2e7"
                android:textColor="#ffffff" />

        </LinearLayout>

    </LinearLayout>
</RelativeLayout>

main_activity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    int color = Color.parseColor("#ff6666");
    toolbar.setBackgroundColor(color);
    setSupportActionBar(toolbar);


    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);


    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);



    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}

1 个答案:

答案 0 :(得分:0)

首先,检查你使用 import android.app.Fragment; 而不是 import android.support.v4.app.Fragment; 的id。 其次,我认为您的问题可能是您需要扩展Fragment而不是直接使用它。我可以提供参考。

  <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="<a href="http://schemas.android.com/apk/res/android"" target="_blank">http://schemas.android.com/apk/res/android"</a>
    package="com.example.maptest1"
    android:versionCode="1"
    android:versionName="1.0" >

    <permission
        android:name="com.example.maptest1.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.example.maptest1.permission.MAPS_RECEIVE" />

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <!-- Required to show current location -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <!-- Required OpenGL ES 2.0. for Maps V2 -->
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <activity
            android:name="com.example.maptest1.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyCnW0xg5nfuJXlpH0dGwysC6VGpZtKNFZM" />
    </application>

</manifest> 
  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="<a href="http://schemas.android.com/apk/res/android"" target="_blank">http://schemas.android.com/apk/res/android"</a>
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <fragment
        android:name="com.google.android.gms.maps.SupportMapFragment"

        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>