我尝试在我的应用程序中使用 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>
错误:
最佳答案
您必须将以下代码粘贴到依赖项上方的应用程序级构建 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/