添加抽屉导航时出错类android.support.design.widget.NavigationView时出错

时间:2017-07-25 17:16:25

标签: android android-gradle

有关此错误的stackoverflow有很多答案,我已阅读其中许多但没有人帮助过我。我正在学习抽屉导航教程。 我尝试了design23.4.0, 23.1.1, 23.2.0版本minSdkVersion 21, 19app:itemTextColor="@color/colorPrimary",但没有任何改变。

我还将NavigationView添加到apply plugin: 'com.android.application' android { compileSdkVersion 23 buildToolsVersion '25.0.2' defaultConfig { applicationId "com.example.nishant.book" minSdkVersion 21 targetSdkVersion 23 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:23.4.0' compile 'com.android.support:support-v4:23.4.0' compile 'com.android.support:design:23.4.0' compile 'com.google.firebase:firebase-database:10.0.1' compile 'com.firebaseui:firebase-ui-auth:1.0.1' compile 'com.android.support.constraint:constraint-layout:1.0.2' testCompile 'junit:junit:4.12' } apply plugin: 'com.google.gms.google-services'

这是gradle:

<?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"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context="com.example.nishant.book.MainActivity">

        <android.support.design.widget.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <include layout="@layout/content_main" />

        </android.support.design.widget.CoordinatorLayout>

    </android.support.design.widget.CoordinatorLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/drawer_header_layout"
        app:menu="@array/drawer_array"
        app:itemTextColor="@color/colorPrimary"
        android:fitsSystemWindows="true">

    </android.support.design.widget.NavigationView>

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

这是activity_main.xml

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nishant.book/com.example.nishant.book.MainActivity}: android.view.InflateException: Binary XML file line #26: Binary XML file line #26: Error inflating class android.support.design.widget.NavigationView
                                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2449)
                                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2509)
                                                                                     at android.app.ActivityThread.access$1000(ActivityThread.java:153)
                                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                     at android.os.Looper.loop(Looper.java:154)
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5529)
                                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
                                                                                  Caused by: android.view.InflateException: Binary XML file line #26: Binary XML file line #26: Error inflating class android.support.design.widget.NavigationView
                                                                                     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 android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
                                                                                     at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:143)
                                                                                     at com.example.nishant.book.MainActivity.createMainView(MainActivity.java:57)
                                                                                     at com.example.nishant.book.MainActivity.onCreate(MainActivity.java:50)
                                                                                     at android.app.Activity.performCreate(Activity.java:6303)
                                                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
                                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2402)
                                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2509) 
                                                                                     at android.app.ActivityThread.access$1000(ActivityThread.java:153) 
                                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373) 
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                     at android.os.Looper.loop(Looper.java:154) 
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5529) 
                                                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739) 
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629) 
                                                                                  Caused by: android.view.InflateException: Binary XML file line #26: Error inflating class android.support.design.widget.NavigationView
                                                                                     at android.view.LayoutInflater.createView(LayoutInflater.java:649)
                                                                                     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:768)
                                                                                     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:708)
                                                                                     at android.view.LayoutInflater.rInflate(LayoutInflater.java:839)
                                                                                     at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:802)
                                                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:519)
                                                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:427) 
                                                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
                                                                                     at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284) 
                                                                                     at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:143) 
                                                                                     at com.example.nishant.book.MainActivity.createMainView(MainActivity.java:57) 
                                                                                     at com.example.nishant.book.MainActivity.onCreate(MainActivity.java:50) 
                                                                                     at android.app.Activity.performCreate(Activity.java:6303) 
                                                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) 
                                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2402) 
                                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2509) 
                                                                                     at android.app.ActivityThread.access$1000(ActivityThread.java:153) 
                                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373) 
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                     at android.os.Looper.loop(Looper.java:154) 
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5529) 
                                                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739) 
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629) 
                                                                                  Caused by: java.lang.reflect.InvocationTargetException
                                                                                     at java.lang.reflect.Constructor.newInstance(Native Method)
                                                                                     at android.view.LayoutInflater.createView(LayoutInflater.java:623)
                                                                                     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:768) 
                                                                                     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:708) 
                                                                                     at android.view.LayoutInflater.rInflate(LayoutInflater.java:839) 
                                                                                     at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:802) 
                                                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:519) 
                                                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:427) 
                                                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
                                                                                     at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284) 
                                                                                     at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:143) 
                                                                                     at com.example.nishant.book.MainActivity.createMainView(MainActivity.java:57) 
                                                                                     at com.example.nishant.book.MainActivity.onCreate(MainActivity.java:50) 
                                                                                     at android.app.Activity.performCreate(Activity.java:6303) 
                                                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) 
                                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2402) 
                                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2509) 
                                                                                     at android.app.ActivityThread.access$1000(ActivityThread.java:153) 
                                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373) 
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                     at android.os.Looper.loop(Looper.java:154) 
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5529) 
                                                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739) 
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629) 
                                                                                  Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f0e0001
                                                                                     at android.content.res.Resources.getValue(Resources.java:1369)
                                                                                     at android.content.res.MiuiResources.getValue(MiuiResources.java:145)
                                                                                     at android.content.res.Resources.loadXmlResourceParser(Resources.java:2824)
                                                                                     at android.content.res.Resources.getLayout(Resources.java:1183)
                                                                                     at android.support.v7.view.SupportMenuInflater.inflate(SupportMenuInflater.java:119)
                                                                                     at android.support.design.widget.NavigationView.inflateMenu(NavigationView.java:250)
                                                                                     at android.support.design.widget.NavigationView.<init>(NavigationView.java:174)
                                                                                     at android.support.design.widget.NavigationView.<init>(NavigationView.java:100)
                                                                                     at java.lang.reflect.Constructor.newInstance(Native Method) 
                                                                                     at android.view.LayoutInflater.createView(LayoutInflater.java:623) 
                                                                                     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:768) 
                                                                                     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:708) 
                                                                                     at android.view.LayoutInflater.rInflate(LayoutInflater.java:839) 
                                                                                     at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:802) 
                                                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:519) 
                                                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:427) 
                                                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
                                                                                     at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284) 
                                                                                     at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:143) 
                                                                                     at com.example.nishant.book.MainActivity.createMainView(MainActivity.java:57) 
                                                                                     at com.example.nishant.book.MainActivity.onCreate(MainActivity.java:50) 
                                                                                     at android.app.Activity.performCreate(Activity.java:6303) 
                                                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) 
                                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2402) 
                                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2509) 
                                                                                     at android.app.ActivityThread.access$1000(ActivityThread.java:153) 
                                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373) 
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                     at android.os.Looper.loop(Looper.java:154) 
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5529) 
                                                                                     at java.lang.reflect.Method.invoke(Native Method)

这是LogCat:

Lambda

1 个答案:

答案 0 :(得分:0)

我认为问题是 app:menu =&#34; @ array / drawer_array&#34; 。你应该传递菜单而不是数组。

导航视图

<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_home"
            app:menu="@menu/activity_home_drawer" />
在res / menu下

,您将拥有activity_home_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_favorites"
            android:icon="@drawable/ic_favorite_white"
            android:title="@string/favorites" />

        <item
            android:id="@+id/nav_about"
            android:icon="@drawable/ic_about"
            android:title="@string/about" />
    </group>


</menu>

请告诉我这是否适合您!