首页 > 解决方案 > 找不到NavigationView如何解决

问题描述

我是android开发的初学者,在使用导航抽屉时遇到了一些困难。我正在开发一个左侧有导航抽屉的应用程序。但是当我将导航视图添加到 Main 活动的 xml 文件中时,它会给出以下错误消息:

Class referenced in the layout file, com.google.android.material.navigation.NavigationView, was not found in the project or the libraries 
Cannot resolve class com.google.android.material.navigation.NavigationView 

这是我的主要活动代码:

package com.hfad.catchat;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Fragment fragment = new InboxFragment();
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(R.id.content_frame, fragment);
        ft.commit();
    }
}

这是主要活动的 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <include
            layout="@layout/toolbar_main"
            android:id="@+id/toolbar" />

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

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/menu_nav" />
</androidx.drawerlayout.widget.DrawerLayout>

标签: androidandroid-studio

解决方案


在“依赖项”中的 build.gradle(app) 中添加

implementation "com.google.android.material:material:1.2.0-alpha02"


推荐阅读