【问题标题】:com.google.android.material.R Cannot resolve symbol 'R' for BottomNavigationViewcom.google.android.material.R 无法为 BottomNavigationView 解析符号“R”
【发布时间】:2019-10-15 17:40:48
【问题描述】:

我在我的应用程序中使用了BottomNavigationView,但我有一个问题:在BottomNavigationView.java 中有import com.google.android.material.R 并且R 是红色的并且写着“无法解析符号'R'”。 我看到的主题对我没有帮助。

你会给我推荐什么?

依赖关系:

    apply plugin: 'com.android.application'
android {
    compileSdkVersion 29
    defaultConfig {
        applicationId "com.example.yourspeakingclub"
        minSdkVersion 24
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    sourceSets {
        main {
            java.srcDirs = ['src/main/java', 'src/main/java/Models', 'src/main/java/com/salendor/yourspeakingclub/models']
        }
    }
    buildToolsVersion = '28.0.3'
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'com.android.support:support-compat'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta2'
    implementation 'com.google.android.gms:play-services-maps:17.0.0'
    testImplementation 'junit:junit:4.13-beta-3'
    androidTestImplementation 'androidx.test:runner:1.3.0-alpha02'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0-alpha02'
    implementation 'com.google.android.material:material:1.1.0-beta01'
}

布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/navigation_container_id"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="?android:attr/actionBarSize">

<com.google.android.material.bottomnavigation.BottomNavigationView

    android:id="@+id/navigation_element"
    android:layout_width="wrap_content"
    android:layout_height="56dp"
    android:layout_gravity="start"
    android:background="?android:attr/windowBackground"
    app:itemTextColor="#000000"
    app:labelVisibilityMode="unlabeled"
    app:itemIconTint="@color/colorBlack"
    app:menu="@menu/bottom_menu" />

</FrameLayout>

“R”问题所在的类定义可在此处找到:https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/bottomnavigation/BottomNavigationView.java

谢谢

【问题讨论】:

    标签: r android-bottomnavigationview


    【解决方案1】:

    我在将 Gradle 更新到最新版本时遇到了这个问题。当我回到 3.1.3 版本时,错误就消失了

    在您的 build.graddle(项目)中更改它

            classpath 'com.android.tools.build:gradle:3.1.3'
    

    【讨论】:

    • 这个解决方案工作得很好,但是当您再次关闭并打开 android studio 时问题仍然存在
    【解决方案2】:

    应该通过 Alt+Enter 并添加正确的导入来解决该错误。但我猜这个问题是在你已经构建并运行了至少一次项目之后突然出现的(因为它曾经发生在我身上)。 这可能是因为您无法引用 XML 布局的元素。在这种情况下,请尝试:

    构建 -> 清理项目

    。 这解决了我的问题。希望对你也有帮助:)

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题。 只需在 build.graddle(项目)文件中更新此工具

      classpath 'com.android.tools.build:gradle:4.1.0'
      

      成功了!!

      【讨论】:

        猜你喜欢
        • 2014-12-21
        • 1970-01-01
        • 1970-01-01
        • 2016-12-31
        • 1970-01-01
        • 2018-04-25
        • 2016-10-26
        • 2023-03-24
        • 1970-01-01
        相关资源
        最近更新 更多