导航NavigationView时出错

时间:2017-07-23 19:25:23

标签: android android-layout android-view android-resources navigationview

我正在试图找出可能导致我的NavigationView崩溃的原因。当我运行这个应用程序时,它说导航我的NavigationView时出错。这是相关的错误信息:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.koeksworld.homenet/com.koeksworld.homenet.HomeNetFeedActivity}: android.view.InflateException: Binary XML file line #52: Error inflating class android.support.design.widget.NavigationView
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2429)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
                                                                        at android.app.ActivityThread.access$800(ActivityThread.java:166)
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                        at android.os.Looper.loop(Looper.java:136)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5590)
                                                                        at java.lang.reflect.Method.invokeNative(Native Method)
                                                                        at java.lang.reflect.Method.invoke(Method.java:515)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
                                                                        at dalvik.system.NativeStart.main(Native Method)
                                                                     Caused by: android.view.InflateException: Binary XML file line #52: Error inflating class android.support.design.widget.NavigationView
                                                                        at android.view.LayoutInflater.createView(LayoutInflater.java:627)
                                                                        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:703)
                                                                        at android.view.LayoutInflater.rInflate(LayoutInflater.java:762)
                                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:499)
                                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
                                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
                                                                        at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
                                                                        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                                                                        at com.koeksworld.homenet.HomeNetFeedActivity.onCreate(HomeNetFeedActivity.java:44)
                                                                        at android.app.Activity.performCreate(Activity.java:5447)
                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2393)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493) 
                                                                        at android.app.ActivityThread.access$800(ActivityThread.java:166) 
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283) 
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                        at android.os.Looper.loop(Looper.java:136) 
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5590) 
                                                                        at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                        at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268) 
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) 
                                                                        at dalvik.system.NativeStart.main(Native Method) 
                                                                     Caused by: java.lang.reflect.InvocationTargetException
                                                                        at java.lang.reflect.Constructor.constructNative(Native Method)
                                                                        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
                                                                        at android.view.LayoutInflater.createView(LayoutInflater.java:601)
                                                                        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:703) 
                                                                        at android.view.LayoutInflater.rInflate(LayoutInflater.java:762) 
                                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:499) 
                                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:398) 
                                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:354) 
                                                                        at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292) 
                                                                        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
                                                                        at com.koeksworld.homenet.HomeNetFeedActivity.onCreate(HomeNetFeedActivity.java:44) 
                                                                        at android.app.Activity.performCreate(Activity.java:5447) 
                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094) 
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2393) 
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493) 
                                                                        at android.app.ActivityThread.access$800(ActivityThread.java:166) 
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283) 
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                        at android.os.Looper.loop(Looper.java:136) 
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5590) 
                                                                        at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                        at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268) 
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) 
                                                                        at dalvik.system.NativeStart.main(Native Method) 
                                                                     Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
                                                                        at android.content.res.Resources.getValue(Resources.java:2329)
                                                                        at android.support.v7.content.res.AppCompatResources.isColorInt(AppCompatResources.java:161)
                                                                        at android.support.v7.content.res.AppCompatResources.inflateColorStateList(AppCompatResources.java:108)
                                                                        at android.support.v7.content.res.AppCompatResources.getColorStateList(AppCompatResources.java:74)
                                                                        at android.support.design.widget.NavigationView.createDefaultColorStateList(NavigationView.java:422)
                                                                        at android.support.design.widget.NavigationView.<init>(NavigationView.java:131)
                                                                        at android.support.design.widget.NavigationView.<init>(NavigationView.java:100)
                                                                        at java.lang.reflect.Constructor.constructNative(Native Method) 
                                                                        at java.lang.reflect.Constructor.newInstance(Constructor.java:423) 
                                                                        at android.view.LayoutInflater.createView(LayoutInflater.java:601) 
                                                                        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:703) 
                                                                        at android.view.LayoutInflater.rInflate(LayoutInflater.java:762) 
                                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:499) 
                                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:398) 
                                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:354) 
                                                                        at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292) 
                                                                        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
                                                                        at com.koeksworld.homenet.HomeNetFeedActivity.onCreate(HomeNetFeedActivity.java:44) 
                                                                        at android.app.Activity.performCreate(Activity.java:5447) 
                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094) 
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2393) 
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493) 
                                                                        at android.app.ActivityThread.access$800(ActivityThread.java:166) 
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283) 
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                        at android.os.Looper.loop(Looper.java:136) 
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5590) 
                                                                        at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                        at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268) 
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) 
                                                                        at dalvik.system.NativeStart.main(Native Method) 

阅读这些信息,我注意到它指向我活动的第52行XML文件 - 第52行是NavigationView的开始标记。这是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"
       tools:context="com.koeksworld.homenet.HomeNetFeedActivity"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:fitsSystemWindows="true"
       android:id = "@+id/FeedDrawerLayout"
        tools:openDrawer="start">
       <LinearLayout
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:orientation="vertical"
           >
           <android.support.v7.widget.Toolbar
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:id = "@+id/HomeNetFeedToolbar"
               android:background="@color/colorPrimary"
               android:elevation="5dp">
               <ImageView
                   android:layout_width="30dp"
                   android:layout_height="30dp"
                   android:layout_marginStart="5dp"
                   android:adjustViewBounds="true"
                   android:id = "@+id/HomeNetFeedToolbarImageView"
                   android:src="@drawable/homenetlogo"
                   />
               <TextView
                   android:layout_width="match_parent"
                   android:layout_height="wrap_content"
                   android:textAlignment="center"
                   android:textSize="18sp"
                   android:textColor="@android:color/white"
                   android:id = "@+id/HomeNetFeedToolbarTextView"/>
           </android.support.v7.widget.Toolbar>
           <FrameLayout
               android:layout_weight="100"
               android:layout_width="match_parent"
               android:layout_height="0dp"
               android:id = "@+id/HomeNetFeedContentView"/>

           <com.roughike.bottombar.BottomBar
               android:layout_width="match_parent"
               android:layout_height="60dp"
               android:id = "@+id/HomeNetFeedBottomBar"
               app:bb_showShadow="true"
               app:bb_tabXmlResource="@xml/homenet_tabs"/>
       </LinearLayout>
    <android.support.design.widget.NavigationView
    android:id = "@+id/HomeNetFeedNavigationMenu"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:menu="@menu/homenet_feed_main_menu"/>
   </android.support.v4.widget.DrawerLayout>

我尝试清理解决方案,Invaliding Cache并重新启动Android Studio,这会导致相同的错误。有没有人有我可以尝试的提示?

更新:这是我的应用Gradle文件

    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.koeksworld.homenet"
        minSdkVersion 19
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.config
        }
        debug {
            signingConfig signingConfigs.config
        }
    }
}

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.twitter.sdk.android:twitter:2.3.2@aar') {
        transitive = true;
    }
    compile('com.github.douglasjunior:android-simple-tooltip:master-SNAPSHOT') {
        changing = true // Gradle will then check for updates every 24 hours
    }

    compile 'com.android.support:multidex:1.0.1'
    compile 'com.android.support:recyclerview-v7:25.3.1'
    compile 'com.android.support:support-v4:25.3.1'
    compile 'id.zelory:compressor:2.0.0'
    compile 'com.android.support:support-v4:25.3.1'
    compile 'com.android.support:recyclerview-v7:25.3.1'
    compile 'com.google.android.gms:play-services-maps:11.0.1'
    compile 'com.google.android.gms:play-services-location:11.0.1'
    compile 'com.squareup.retrofit2:retrofit:2.3.0'
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.google.firebase:firebase-crash:11.0.1'
    compile 'com.google.firebase:firebase-core:11.0.1'
    compile 'com.google.firebase:firebase-messaging:11.0.1'
    compile 'com.squareup.retrofit2:converter-gson:2.3.0'
    compile 'com.github.PhilJay:MPAndroidChart:v3.0.2'
    compile 'com.android.support:cardview-v7:25.3.1'
    compile 'com.facebook.android:facebook-android-sdk:4.+'
    compile 'com.android.support:customtabs:25.3.1'
    compile 'com.android.support:design:25.3.1'
    compile 'com.jaredrummler:material-spinner:1.1.0'
    compile 'com.roughike:bottom-bar:2.3.1'
    compile 'com.android.support:percent:25.3.1'
    compile 'it.sephiroth.android.library.targettooltip:target-tooltip-library:1.3.15'
    compile 'com.android.support.constraint:constraint-layout:1.0.0-beta5'
    compile 'fr.avianey.com.viewpagerindicator:library:2.4.1.1@aar'
    compile 'com.android.support:support-v13:25.3.1'
    compile 'com.daimajia.swipelayout:library:1.2.0@aar'
    compile 'com.github.bumptech.glide:glide:4.0.0-RC1'
    compile 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1'
    testCompile 'junit:junit:4.12'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.0.0-RC1'
}

apply plugin: 'com.google.gms.google-services'
apply plugin: 'realm-android'

**这是我的导航标题xml文件(homenet_feed_menu_header.xml):**

    ?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:id="@+id/HeaderLayout"
    android:background="@drawable/background"
    >
    <ImageView
        android:id = "@+id/UserProfileImageView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_widthPercent="30%"
        app:layout_heightPercent="40%"
        android:layout_marginStart="5dp"
        android:layout_marginTop="10dp"
        android:src="@color/colorAccent"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"/>
    <TextView
        android:id = "@+id/HeaderEmailAddressTextView"
        android:text="@string/sample_email_string"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"
        android:textSize="16sp"
        android:layout_margin="5dp"
        android:layout_alignParentBottom="true"/>
    <TextView
        android:id = "@+id/UserNameSurnameTextView"
        android:text="@string/sample_name_string"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_margin="5dp"
        android:textSize="18sp"
        android:textColor="@android:color/white"
        android:layout_above="@id/HeaderEmailAddressTextView"/>
    </android.support.percent.PercentRelativeLayout>

这是我的菜单资源文件(homenet_feed_main_menu.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/ViewProfileOption"
    android:title="@string/view_profile_string"
    android:icon="@drawable/ic_person_black_24dp"
    />
    <item
        android:id = "@+id/EditProfileOption"
        android:checked="false"
        android:title="@string/edit_profile_string"
        android:icon="@drawable/ic_mode_edit_black_24dp"/>
    <item
        android:id = "@+id/SearchOption"
        android:checked="false"
        android:title="@string/search_string"
        android:icon="@drawable/ic_search_black_24dp"/>

    <item
        android:id = "@+id/HomeManager"
        android:checked="false"
        android:title="@string/home_manager_string"
        android:icon ="@drawable/ic_home_black_24dp"/>
    <item
        android:id = "@+id/AnnouncementsOption"
        android:title="@string/announcements_string"
        android:checked="false"
        android:icon="@drawable/ic_announcement_black_24dp"/>

    <item
        android:id = "@+id/SettingsOption"
        android:title="@string/settings_string"
        android:checked="false"
        android:icon="@drawable/screwdriver"/>
    </group>
</menu>

3 个答案:

答案 0 :(得分:0)

app:headerLayout="@layout/nav_header_navigation"

由于您的xml代码没有headerLayout。请尝试包含此项或检查它是否有不同的名称,但默认情况下会在包含活动时给出。

答案 1 :(得分:0)

试试这个

 <android.support.v4.widget.DrawerLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:focusableInTouchMode="true">

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

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <android.support.v7.widget.Toolbar
                android:id="@+id/tool_bar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:layout_scrollFlags="enterAlways"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
        </android.support.design.widget.AppBarLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
            // Your Content

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

    <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:background="@android:color/white"
        android:fitsSystemWindows="false"
        app:headerLayout="@layout/drawer_header" //Set header
        app:menu="@menu/drawer" />
</android.support.v4.widget.DrawerLayout>

同时检查 - Navigation Drawer - how to change color of tint background?

答案 2 :(得分:0)

在我看来,你的活动主题存在问题。

appcompatish 主题之一应用于您的活动/应用程序:

<application
    android:theme="@style/Theme.AppCompat.Light.NoActionBar"
    ... >