NavigationView出现错误:在包中找不到属性“menu”的资源标识符。

16

我正在尝试将NavigationView添加到我的布局中,如下所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">


    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />

        <android.support.design.widget.NavigationView
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:menu="@menu/drawer" />

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

但问题在于编译时我遇到了以下错误:

C:\Users\IBM_ADMIN\Documents\Androidprojects\supporttest\app\src\main\res\layout\activity_main.xml
Error:(22) No resource identifier found for attribute 'menu' in package 'ranjithnair02.com.supporttest'
Error:Execution failed for task ':app:processDebugResources'.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Users\IBM_ADMIN\Documents\android-studio\sdk\build-tools\22.0.1\aapt.exe'' finished with non-zero exit value 1
Information:BUILD FAILED

我已经在menu/drawer.xml中添加了菜单项。

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

    <group android:checkableBehavior="single">

        <item
            android:id="@+id/navigation_item_1"
            android:checked="true"
            android:icon="@drawable/abc_tab_indicator_mtrl_alpha"
            android:title="First" />

        <item
            android:id="@+id/navigation_item_2"
            android:icon="@drawable/abc_btn_check_material"
            android:title="Second" />
      </group>
</menu>

https://pratamawijaya.com/programming/android-programming/after-google-io-2015-android-design-support-library-demo - M D
你是否添加了Gradle依赖项?compile 'com.android.support:design:22.2.0' - hungryghost
你尝试过清理项目吗?关闭并重新打开Android Studio?这对我有帮助。 - Nick H
2个回答

46

确保您使用正确的Android Design Support Library依赖项。 选择错误很容易 - 因为谷歌发布了两个不同的依赖字符串:

截至此答案的时间,Android开发者博客(以及用户hungryghost的评论)有正确的依赖字符串,而支持库主页上的依赖字符串无法工作。

请使用以下依赖项: compile 'com.android.support:design:26.1.0'

请注意版本号是22.2.0(错误:22.0.0),包名为design(错误:support-design

在更改gradle文件后,请使用Android Studio菜单中的“重建项目”或出现的“同步”按钮。 这样最终可以使其正常工作。


难道com.android.support:appcompat-v7不包括设计库吗? - IgorGanapolsky
1
@IgorGanapolsky,当在依赖于appcompat-v7的测试项目上使用maven(mvn dependency:tree)时,情况并非如此。此外,在支持库网站上也没有任何迹象:https://developer.android.com/topic/libraries/support-library/packages.html - Patrick

8

将以下代码添加到应用 gradle 文件中:

dependencies { .....

  • implementation 'com.android.support:design:26.1.0'

....

}

适用于 gradle 版本 3.0.1。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接