首页 > 解决方案 > 无法解析导入android.support.v7.widget.RecyclerView;

问题描述

我需要在 Android Studio 中创建一个 recyclerview。我不知道为什么它一直说它无法解析符号 recyclerview。我已经根据那些网站教程完成了所有步骤。

我的应用程序 gradle 文件:

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.0"

    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 17
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    gradle.projectsEvaluated {
        tasks.withType(JavaCompile) {
            options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.google.firebase:firebase-core:17.0.0'
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

    implementation 'com.android.support:appcompat-v7:29.1.1'
    //card view
    implementation 'com.android.support:cardview-v7:29.1.1'
    //recyclerView
    implementation 'com.android.support:recyclerview-v7:29.1.1'
    //picasso library to retrive images
    implementation 'com.squareup.picasso:picasso:2.71828'


    implementation 'com.google.firebase:firebase-storage:18.0.0'

    implementation 'com.google.firebase:firebase-auth:18.0.0'
    implementation 'com.google.firebase:firebase-database:18.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    apply plugin: 'com.google.gms.google-services'

}

我可以知道我的 gradle 文件中的代码有什么问题吗?提前致谢。这是我在 logcat 中的错误报告:

错误

标签: androidandroid-studioandroid-recyclerview

解决方案


您正在尝试访问错误的包。

尝试 androidx.recyclerview 而不是旧的 android.support.v7.widget.RecyclerView;

您应该替换所有 com.android.support.* 依赖项,并替换为 androidx 等效项。(目前,您同时拥有 androidx 和旧版 android.support 依赖项)

https://developer.android.com/jetpack/androidx/releases/recyclerview

您可以尝试使用 Android Studio 上的 Refactor> Migrate to AndroidX 选项进行自动修复

在此处输入图像描述


推荐阅读