【问题标题】:Failed to instantiate one or more class (Recyclerview not working)无法实例化一个或多个类(Recyclerview 不工作)
【发布时间】:2020-02-24 04:06:34
【问题描述】:

每当我尝试插入 recyclerview 时,它都无法正常工作并显示相同的错误,现在我不知道如何删除它,请快速帮助我。当我尝试在布局中添加cardview 时,也会出现同样的问题。

我的 API 是 29

SDK 版本为 28

我什至尝试减少我的 API,但仍然没有任何反应

错误:

java.lang.NoClassDefFoundError: androidx/recyclerview/R$attr
    at androidx.recyclerview.widget.RecyclerView.<init>(RecyclerView.java:650)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.jetbrains.android.uipreview.ViewLoader.createNewInstance(ViewLoader.java:403)
    at org.jetbrains.android.uipreview.ViewLoader.loadClass(ViewLoader.java:186)
    at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:144)
    at com.android.tools.idea.rendering.LayoutlibCallbackImpl.loadView(LayoutlibCallbackImpl.java:309)
    at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:418)
    at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:429)
    at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:333)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:863)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:837)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:323)
    at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:394)
    at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:200)
    at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:572)
    at com.android.tools.idea.rendering.RenderTask.lambda$inflate$5(RenderTask.java:698)
    at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1590)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)



错误(警告)

This operation requires the library androidx.recyclerview:recyclerview:+.

Problem: Inconsistencies in the existing project dependencies found.
Version incompatibility between:
-   androidx.appcompat:appcompat:1.1.0@aar
and:
-   androidx.appcompat:appcompat:1.1.0@aar

With the dependency:
-   androidx.annotation:*:1.1.0
versus:
-   androidx.annotation:*:2.0.0

The project may not compile after adding this library.
Would you like to add it anyway?

build.gradle(app)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.example.voiceprescription"
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        android.defaultConfig.vectorDrawables.useSupportLibrary = true
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    def appcompat_version = "1.1.0"

    implementation "androidx.appcompat:appcompat:$appcompat_version"
    implementation "androidx.appcompat:appcompat-resources:$appcompat_version"
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.android.support:recyclerview-v7:28.0.0'
    implementation "androidx.cardview:cardview:1.0.0"

    implementation 'com.google.firebase:firebase-auth:19.2.0'
    implementation 'com.google.firebase:firebase-database:19.2.1'

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}
apply plugin: 'com.google.gms.google-services'

build.grade(项目)

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        google()
        jcenter()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.3'
        classpath 'com.google.gms:google-services:4.3.3'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()

    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

【问题讨论】:

  • 发布您的错误。
  • 我添加了你现在可以看到

标签: android xml android-studio android-layout android-recyclerview


【解决方案1】:

在您的项目中,所有其他组件都迁移到 androidx,除了 recyclerView

改变

com.android.support:recyclerview-v7:28.0.0

收件人

androidx.recyclerview:recyclerview:1.1.0

在你的 build.gradle(app)

【讨论】:

  • 在你的布局中使用&lt;androidx.recyclerview.widget.RecyclerView/&gt;
【解决方案2】:

问题是您没有使用过 Androidx recyclerviewgladle 插件
使用这个
implementation "androidx.recyclerview:recyclerview:1.1.0"
而不是这个
implementation 'com.android.support:recyclerview-v7:28.0.0'
阅读有关 recyclerview here 的信息。

【讨论】:

  • 尝试清除缓存和干净构建,也希望您在 xml 布局中使用了正确的。
【解决方案3】:

使用这个依赖而不是任何特定的基于视图的依赖

implementation 'com.android.support:design:28.0.0'

以上依赖项适用于所有遗留组件/Android 视图:)

【讨论】:

    【解决方案4】:

    请使用

    implementation 'androidx.recyclerview:recyclerview:1.1.0'
    

    【讨论】:

    • 你能分享你的 git 链接来检查我的电脑吗?
    猜你喜欢
    • 1970-01-01
    • 2017-12-31
    • 2019-01-25
    • 1970-01-01
    • 2020-06-24
    • 1970-01-01
    • 2017-10-13
    • 2020-08-10
    • 1970-01-01
    相关资源
    最近更新 更多