java - RecyclerView在最新版本中无法实例化。 Android Studio 的

标签 java android android-studio android-recyclerview

我尝试在我的应用程序中使用 RecyclerView。 我遇到以下问题:

“无法实例化以下类: android.support.v7.widget.recyclerview 提示:使用 View.IsInEditMode()"

我在这个论坛上阅读了很多文章,这些文章帮助了很多用户,但对我来说,这些方法在我的程序中不起作用。

我将 Android Studio 更新到了最新版本:Beta 4 (Canary) 和 Sdk 版本,但我不确定我的 build.grade 是否已正确更新。

我认为这可能是由这个 Android Studio 的某些不稳定版本引起的,但我不确定。很多人通过更新软件来解决这个问题。

当我设置 API23 时,它可以编译,但在我的 API 21 (Lollipop) 上不起作用。

有人可以帮我解决这个问题吗?

我的build.grade:

compileSdkVersion 23
buildToolsVersion "23.0.2"

defaultConfig {
    applicationId "com.example.karol.material_test"
    minSdkVersion 21
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}
dependencies {

compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:support-v4:23.1.1'
compile 'com.android.support:recyclerview-v7:23.1.1'

}

布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#3D5AFE"
    tools:context="com.example.karol.material_test.NavigationDrawerFragment">

    <!-- TODO: Update blank fragment layout -->
    <LinearLayout
        android:id="@+id/containerDrawerImage"
        android:layout_width="match_parent"
        android:background="#BBDEFB"
        android:layout_height="wrap_content">
    <ImageView

        android:layout_marginTop="@dimen/margin_top"
        android:layout_width="140dp"
        android:layout_height="140dp"
        android:src="@drawable/karol"/>
    </LinearLayout>

    <android.support.v7.widget.RecyclerView
    android:id="@+id/drawerList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    </android.support.v7.widget.RecyclerView>

</RelativeLayout>

错误:

enter image description here

最佳答案

您必须将以下代码粘贴到依赖项上方的应用程序级构建 gradle 文件中

        configurations.all {
            resolutionStrategy.eachDependency { DependencyResolveDetails details ->
                def requested = details.requested
                if (requested.group == 'com.android.support') {
                    if (!requested.name.startsWith("multidex")) {
                        details.useVersion '26.1.0'
                    }
                }
            }
        }
        android {
            packagingOptions {
                exclude 'META-INF/DEPENDENCIES.txt'
                exclude 'META-INF/LICENSE.txt'
                exclude 'META-INF/NOTICE.txt'
                exclude 'META-INF/NOTICE'
                exclude 'META-INF/LICENSE'
                exclude 'META-INF/DEPENDENCIES'
                exclude 'META-INF/notice.txt'
                exclude 'META-INF/license.txt'
                exclude 'META-INF/dependencies.txt'
                exclude 'META-INF/LGPL2.1'
            }
        }

    dependencies {

compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:support-v4:23.1.1'
compile 'com.android.support:recyclerview-v7:23.1.1'

}

        implementation 'com.android.support:cardview-v7:26.1.0'
        implementation 'com.android.support:recyclerview-v7:26.1.0'

    }

关于java - RecyclerView在最新版本中无法实例化。 Android Studio 的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35395179/

相关文章:

java - Spring Boot Controller 未被映射

java - 基于 JPA 的 JUnit 测试最佳实践

java - 向字符串添加空行

c# - 以编程方式读取汽车里程表和其他汽车信息

java - 停止媒体播放器

android - 错误 :Execution failed for task ':app:processDebugGoogleServices' - com. google.android.gms

java - 如何从文件中获取图像和图像名称?

java - 等待锁释放的方法是怎样的?

java - 从文件读取不会读取第一个值Java

Android SearchView 设置默认展开