【问题标题】:Android Studio RecyclerView import problemAndroid Studio RecyclerView 导入问题
【发布时间】:2020-02-10 12:10:58
【问题描述】:

我目前在尝试为我的项目导入回收站视图时收到错误消息。

import android.support.v7.widget.RecyclerView;

这是我的 build.gradle:

apply plugin: 'com.android.application'
android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.example.zooapp"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        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'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
    implementation 'androidx.recyclerview:recyclerview:1.1.0'
    implementation 'com.google.android.gms:play-services-maps:16.0.0'
    implementation 'com.android.support:recyclerview-v7:29.0.0'
}

这是我的代码的开始:

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;


public class AnimalsHolder extends RecyclerView.ViewHolder
        implements View.OnClickListener {

    private final ImageView animalIcon;
    private final TextView animalName;

当它是这样的时候,我尝试点击一个按钮来打开一个新的活动(应该是回收器视图),应用程序就会崩溃并说:

android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class android.support.v7.widget.RecyclerView
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)

然后,当我删除 androidx 并添加 V7 必须存在的导入时。

import android.support.v7.widget.RecyclerView;

v7 变红,extend RecyclerView.ViewHolder 也变红。 如果我尝试按 ALT + ENTER 再次导入,则会出现 androidx。

由于这是来自大学的实践,它应该与 android.support.v7 一起使用。

【问题讨论】:

  • 您必须将其迁移到 AppCompat 才能运行应用程序

标签: android android-studio android-recyclerview import widget


【解决方案1】:

将您的 recyclerView 块更改为以下代码 如果你使用 androidx 依赖 RecyclerView

<androidx.recyclerview.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</androidx.recyclerview.widget.RecyclerView>

【讨论】:

  • @Valeri Vladimirov 欢迎?
【解决方案2】:

如果你想在你的项目中使用 AndroidX

  1. 打开 Android Studio
  2. 点击重构
  3. 点击迁移到AndroidX
  4. 它会要求您先制作一个 zip。点击确定

如果你想在你的项目中使用 AppCompat

  1. 打开 Android Studio
  2. 点击重构
  3. 点击迁移到 AppCompat
  4. 它会要求您先制作一个 zip。点击确定

【讨论】:

    【解决方案3】:

    链接https://developer.android.com/jetpack/androidx/releases/recyclerview#1.1.0

     //Add this
      dependencies
      {
       implementation "androidx.recyclerview:recyclerview:1.1.0"
       // For control over item selection of both touch and mouse driven selection
       implementation "androidx.recyclerview:recyclerview-selection:1.1.0-rc01"
      }
    

    //在xml中

    <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rv_defulterList"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    </androidx.recyclerview.widget.RecyclerView>
    

    【讨论】:

      【解决方案4】:

      我能够解决在应用程序 gradle 文件中添加此依赖项的问题:

       implementation 'androidx.recyclerview:recyclerview:1.1.0'
      

      【讨论】:

        猜你喜欢
        • 2016-12-23
        • 2020-06-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-21
        • 2018-12-26
        • 1970-01-01
        相关资源
        最近更新 更多