android - 找不到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>
解决方案
在“依赖项”中的 build.gradle(app) 中添加
implementation "com.google.android.material:material:1.2.0-alpha02"
推荐阅读
- bash - 通过 bash 脚本运行工具
- java - 如何使用 escpos-2.2 jar & jSerialComm-2.5.0 jar 在热敏收据打印机上打印收据
- python-3.x - KivyMD 中的 MDDropDownMenu 错误(适用于 Python 3.6 上的 Kivy)
- javascript - vue2-google-maps 如何使用 Embed API “放置模式”
- pdf - PDFBOX - 签署 PDF 时,不调用 SignatureInterface 的签名
- php - PHP 和 SQL 中的日期转换
- aspnetboilerplate - 如何取消 BackgroundJob 或让新作业不重试?
- opencv - OpenCV:将 .raw(通过 photoshop cs6)转换为 Mat 返回灰色
- doctrine-orm - 脚本缓存:清除返回错误代码 255 致命错误:未捕获错误:类 'Doctrine\Bundle\DoctrineCacheBundle\DoctrineC
- javascript - 显示内联块在 Safari 上不起作用