Android开发:使用BottomNavigationView出现Cannot resolve class错误
我在Android应用开发中使用BottomNavigationView组件时,遇到错误提示:Cannot resolve class com.google.android.material.bottomnavigation.BottomNavigationView,错误出现在布局文件的BottomNavigationView声明位置。以下是相关代码:
Gradle配置
apply plugin: 'com.android.application' android { compileSdkVersion 34 buildToolsVersion '34.0.0' defaultConfig { applicationId "com.pfsol.tfpmobileassetsmanagament" minSdkVersion 21 targetSdkVersion 34 versionCode 1 versionName "1.0" testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner' vectorDrawables.useSupportLibrary = true multiDexEnabled true } tasks.withType(JavaCompile).configureEach{ options.fork = true options.forkOptions.jvmArgs +=[ '--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED', '--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED', '--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED'] } buildTypes { release { minifyEnabled false debuggable false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } lintOptions { checkReleaseBuilds false } namespace 'com.pfsol.tfpmobileassetsmanagament' } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) androidTestImplementation('androidx.test.espresso:espresso-core:3.5.1', { exclude group: 'com.android.support', module: 'support-annotations' }) implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'com.google.android.material:material:1.11.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation 'com.squareup.retrofit2:retrofit:2.9.0' implementation 'com.squareup.retrofit2:converter-gson:2.9.0' implementation 'com.jakewharton:butterknife:10.2.3' implementation 'androidx.cardview:cardview:1.0.0' implementation 'androidx.legacy:legacy-support-v4:1.0.0' implementation 'androidx.vectordrawable:vectordrawable:1.1.0' implementation 'com.squareup.picasso:picasso:2.71828' testImplementation 'junit:junit:4.13.2' implementation 'androidx.recyclerview:recyclerview:1.3.2' implementation 'com.google.zxing:core:3.4.1' implementation 'com.journeyapps:zxing-android-embedded:3.6.0' annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.3' implementation 'com.squareup.okhttp3:logging-interceptor:3.9.1' //implementation 'com.github.luongvo:GmailBackground:2.0.2' implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0' // Use the appropriate version implementation 'androidx.drawerlayout:drawerlayout:1.2.0' // Use the appropriate version // implementation 'com.intsig.camscanner:CamScannerSdk:4.1.0.20190920' implementation 'io.github.mayuce:AndroidDocumentScanner:1.6.1' // required to avoid crash on Android 12 API 31 implementation 'androidx.work:work-runtime-ktx:2.9.0' implementation 'com.android.billingclient:billing:6.1.0' implementation 'com.google.android.material:material:1.11.0' }
布局文件
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <!-- Your other layout elements --> <com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/bottom_navigation" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" app:menu="@menu/activity_main_drawer" /> </androidx.constraintlayout.widget.ConstraintLayout>
解决步骤
- 重新同步Gradle依赖:点击Android Studio右上角的
Sync Project with Gradle Files按钮,确保Material库正确下载并引入项目。 - 清理并重建项目:执行
Build > Clean Project,随后执行Build > Rebuild Project,解决编译缓存导致的类识别问题。 - 清除IDE缓存:打开
File > Invalidate Caches...,选择Invalidate and Restart,修复Android Studio的类索引异常。 - 修正布局约束(可选):由于BottomNavigationView在ConstraintLayout中,
android:layout_gravity="bottom"不生效,需添加ConstraintLayout约束使其固定在底部:<com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/bottom_navigation" android:layout_width="match_parent" android:layout_height="wrap_content" app:menu="@menu/activity_main_drawer" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" />
内容的提问来源于stack exchange,提问作者Moeez
相关产品推荐
相关产品推荐

