【问题标题】:RecyclerView could not be instantiated in the newest ver. of Android StudioRecyclerView 无法在最新版本中实例化。安卓工作室
【发布时间】:2016-05-25 12:37:01
【问题描述】:

我尝试在我的应用中使用 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>

错误:

【问题讨论】:

  • 请显示完整的错误消息,并附上您看到的上下文。还要显示您添加回收站视图的布局。
  • @DougStevenson,我添加了这个布局和错误。你在我的代码中看到了什么错误吗?
  • @war_Hero 我已经做过很多次了,但它并没有改善我的程序。
  • 尝试删除现有的导入,然后再做一次
  • @war_Hero 是什么意思?我应该删除此布局或项目吗?对不起,我不明白你的说法。

标签: java android android-studio android-recyclerview


【解决方案1】:

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

        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'

    }

【讨论】:

  • 你好。欢迎来到 Stackoverflow。还请解释 OP 的代码有什么问题以及该代码将如何提供帮助。复制粘贴代码不会帮助任何人学习和变得更好!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-27
  • 1970-01-01
  • 2013-07-08
  • 2017-04-17
  • 2014-04-30
相关资源
最近更新 更多