android - 尝试使用 RecyclerView 时出错 - 为什么?
问题描述
我是一名新的 Android Studio 开发人员,我正在研究如何使用 RecyclerView,但使用我在 google 和 youtube 上搜索的所有教程似乎无法使其工作。当我尝试运行我在本网站上复制的代码时显示以下错误 - https://medium.com/android-grid/how-to-use-firebaserecycleradpater-with-latest-firebase-dependencies-in-android-aff7a33adb8b
请帮忙。我正在使用最新的 AndroidStudio 版本。
错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.itbscorp.testrecycler, PID: 18467
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.itbscorp.testrecycler/com.itbscorp.testrecycler.MainActivity}: android.view.InflateException: Binary XML file line #33: Binary XML file line #33: Error inflating class android.support.v7.widget.RecyclerView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: android.view.InflateException: Binary XML file line #33: Binary XML file line #33: Error inflating class android.support.v7.widget.RecyclerView
Caused by: android.view.InflateException: Binary XML file line #33: Error inflating class android.support.v7.widget.RecyclerView
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v7.widget.RecyclerView" on path: DexPathList[[zip file "/data/app/com.itbscorp.testrecycler-kbheTQbZQdBBMxi8OmT9Ew==/base.apk"],nativeLibraryDirectories=[/data/app/com.itbscorp.testrecycler-kbheTQbZQdBBMxi8OmT9Ew==/lib/x86, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at android.view.LayoutInflater.createView(LayoutInflater.java:606)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
at com.itbscorp.testrecycler.MainActivity.onCreate(MainActivity.java:43)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
MainActivity.java
public class Model {
public String mId, mTitle, mDesc;
public Model() {
}
public Model(String mId, String mTitle, String mDesc) {
this.mId = mId;
this.mTitle = mTitle;
this.mDesc = mDesc;
}
public String getmId() {
return mId;
}
public void setmId(String mId) {
this.mId = mId;
}
public String getmTitle() {
return mTitle;
}
public void setmTitle(String mTitle) {
this.mTitle = mTitle;
}
public String getmDesc() {
return mDesc;
}
public void setmDesc(String mDesc) {
this.mDesc = mDesc;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>a
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="@+id/et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:layout_gravity="center" />
<EditText
android:id="@+id/etd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:layout_gravity="center" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
模型.java
public class Model {
public String mId, mTitle, mDesc;
public Model() {
}
public Model(String mId, String mTitle, String mDesc) {
this.mId = mId;
this.mTitle = mTitle;
this.mDesc = mDesc;
}
public String getmId() {
return mId;
}
public void setmId(String mId) {
this.mId = mId;
}
public String getmTitle() {
return mTitle;
}
public void setmTitle(String mTitle) {
this.mTitle = mTitle;
}
public String getmDesc() {
return mDesc;
}
public void setmDesc(String mDesc) {
this.mDesc = mDesc;
}
}
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp">
<LinearLayout
android:id="@+id/list_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:orientation="vertical"
android:background="?attr/selectableItemBackground"
android:padding="16dp">
<TextView
android:id="@+id/list_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title"
android:textColor="#000000"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/list_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:text="Description" />
</LinearLayout>
</android.support.v7.widget.CardView>
build.grade 文件
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.itbscorp.testrecycler"
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.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.firebaseui:firebase-ui-database:6.1.0'
implementation 'com.firebaseui:firebase-ui-firestore:6.1.0'
implementation 'com.firebaseui:firebase-ui-auth:6.1.0'
implementation 'com.firebaseui:firebase-ui-storage:6.1.0'
implementation 'com.google.firebase:firebase-database:16.0.4'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
解决方案
您正在使用 android.support.v7.widget.RecyclerView,它已过时,也未包含在您的项目中。
确保在 build.gradle 中包含 android.support.v7.widget.RecyclerView 或使用 androidx recyclerview 库进行重构。
对于旧的支持库使用:
implementation 'com.android.support:recyclerview-v7:28.0.0'
对于 AndroidX:
implementation 'androidx.recyclerview:recyclerview:1.1.0'
并使用 androidx.recyclerview.widget.RecyclerView 在您的 xml 中更改 android.support.v7.widget.RecyclerView。
推荐阅读
- javascript - react-native-maps :如何在放大和缩小时获取动态 latitudeDelta 和 longitudeDelta 值
- html - 标记在网格中移动项目
- nearprotocol - 如何使用 cli 获得账户余额?
- sql - 寻找两个人之间关系的最佳数据库设计
- image - 手机上不显示英雄图片
- api - 如何以这种格式读取 Kotlin 中的 REST 数据并将其放入数据类中
- haskell - 是否有用于编译时检查的示例值以及它们应该在代码中的什么位置?
- r - 部署 R 闪亮应用程序后出错(找不到对象)
- reactjs - 在 React Firebase Hooks 中使用自定义声明来访问特定的数据库 Ref
- django - 按插入的顺序值排序多对多字段