【问题标题】:How to fix "Android Resource linking failed"? using NavigationView and Fragments如何修复“Android 资源链接失败”?使用 NavigationView 和 Fragments
【发布时间】:2019-02-18 17:39:30
【问题描述】:

我想为我的 Android Studio 应用程序使用汉堡菜单,我正在尝试按照本教程进行操作:https://github.com/codepath/android_guides/wiki/Fragment-Navigation-Drawer

在我的activity_main.xml 文件中我有这个错误:

Android resource linking failed 
E:\Smoozy\Documents\Findcontent\app\src\main\res\layout\activity_main.xml:76:
 error: resource menu/drawer_view (aka com.example.findcontent:menu/drawer_view) not found.
error: failed linking file resources.

这是我的文件目录: https://imgur.com/a/8bqT9rY

我的drawer_view.xml 在我的布局文件夹中,我尝试从app:menu="@menu/drawer_view"/> 代码语句中删除“菜单”。

 <!-- This DrawerLayout has two children at the root  -->
<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">
    <!-- The navigation drawer that comes from the left -->
    <!-- Note that `android:layout_gravity` needs to be set to 'start' -->
    <android.support.design.widget.NavigationView
        android:id="@+id/nvView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:layout_weight="1"
        android:background="@android:color/white"
        app:menu="@menu/drawer_view"/>

    <!-- This LinearLayout represents the contents of the screen  -->
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <!-- The ActionBar displayed at the top -->
        <include
            layout="@layout/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <!-- The main content view where fragments are loaded -->
        <FrameLayout
            android:id="@+id/flContent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </FrameLayout>


</android.support.v4.widget.DrawerLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

这是试图到达它的代码:

public void selectDrawerItem(MenuItem menuItem) {
    // Create a new fragment and specify the fragment to show based on     nav item clicked
    Fragment fragment = null;
    Class fragmentClass;
    switch(menuItem.getItemId()) {
        case R.id.nav_first_fragment:
            fragmentClass = FirstFragment.class;
               getSupportFragmentManager().beginTransaction().replace(R.id.flContent, new FirstFragment()).commit();

            break;
        case R.id.nav_second_fragment:
            fragmentClass = SecondFragment.class;
            getSupportFragmentManager().beginTransaction().replace(R.id.flContent, new SecondFragment()).commit();
            break;
        case R.id.nav_third_fragment:
            fragmentClass = ThirdFragment.class;
            getSupportFragmentManager().beginTransaction().replace(R.id.flContent, new ThirdFragment()).commit();
            break;
        default:
            fragmentClass = FirstFragment.class;
    }

    try {
        fragment = (Fragment) fragmentClass.newInstance();
    } catch (Exception e) {
        e.printStackTrace();
    }

    // Insert the fragment by replacing any existing fragment
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();

    // Highlight the selected item has been done by NavigationView
    menuItem.setChecked(true);
    // Set action bar title
    setTitle(menuItem.getTitle());
    // Close the navigation drawer
    mDrawer.closeDrawers();
}

// ...

谢谢!

【问题讨论】:

  • 您需要将drawer_view布局添加到res/menu目录。
  • 谢谢! @ישואוהבאותך

标签: java android hamburger-menu


【解决方案1】:

我认为您需要将“drawer_view.xml”文件移动到“menu”目录中但您没有。所以只需创建,右键单击res然后单击new -> new resource directory,选择对于资源类型菜单,然后按“确定”。 通常你现在会看到一个菜单文件夹

【讨论】:

    【解决方案2】:

    转到 res 并右键单击 New > Android Resources Directory ok
    名称:菜单
    转到drawable并将文件'drawer_view.xml'剪切到menu文件夹
    并更改代码中的路径。
    希望能帮到你。

    【讨论】:

    • 在这种情况下 app:menu="@menu/drawer_view"
    猜你喜欢
    • 2020-01-03
    • 2019-06-06
    • 1970-01-01
    • 2019-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-09
    • 1970-01-01
    相关资源
    最近更新 更多