Android 尝试使用RecyclerView时出错-为什么?

Android 尝试使用RecyclerView时出错-为什么?,android,android-recyclerview,Android,Android Recyclerview,我是一名新的Android Studio开发人员,我正在研究如何使用RecyclerView,但使用我在google和youtube上搜索的所有教程,我似乎无法让它正常工作。当我尝试运行我在此网站上复制的代码时,会显示以下错误- 请帮忙。我正在使用最新的AndroidStudio版本 错误: E/AndroidRuntime: FATAL EXCEPTION: main Process: com.itbscorp.testrecycler, PID: 18467 java.lan

我是一名新的Android Studio开发人员,我正在研究如何使用RecyclerView,但使用我在google和youtube上搜索的所有教程,我似乎无法让它正常工作。当我尝试运行我在此网站上复制的代码时,会显示以下错误-

请帮忙。我正在使用最新的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>
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>

您使用的是android.support.v7.widget.RecyclerView,它已经过时,也未包含在您的项目中

请确保在build.gradle中包含android.support.v7.widget.RecyclerView,或者使用androidx RecyclerView库进行重构

对于旧的支持库,请使用:
implementation'com.android.support:recyclerview-v7:28.0.0'

对于AndroidX:
实现“androidx.recyclerview:recyclerview:1.1.0”

并使用androidx.RecyclerView.widget.RecyclerView更改xml中的android.support.v7.widget.RecyclerView。

您使用的是android.support.v7.widget.RecyclerView,它已过时,也未包含在项目中

请确保在build.gradle中包含android.support.v7.widget.RecyclerView,或者使用androidx RecyclerView库进行重构

对于旧的支持库,请使用:
implementation'com.android.support:recyclerview-v7:28.0.0'

对于AndroidX:
实现“androidx.recyclerview:recyclerview:1.1.0”
并使用androidx.RecyclerView.widget.RecyclerView在xml中更改android.support.v7.widget.RecyclerView。

您应该更改
android.support.v7.widget.RecyclerView
在您的xml中添加到
androidx.RecyclerView.widget.RecyclerView.

在项目依赖项中,检查是否为recyclerview设置了正确的依赖项

实现“androidx.recyclerview:recyclerview:1.1.0”

您应该更改
android.support.v7.widget.RecyclerView
在您的xml中添加到
androidx.RecyclerView.widget.RecyclerView.

在项目依赖项中,检查是否为recyclerview设置了正确的依赖项


实现“androidx.recyclerview:recyclerview:1.1.0”

请附上您的代码添加更多代码细节,布局和渐变发布xml代码问题似乎出在项目的依赖项中,因为它在类路径中找不到RecyclerView:android.view.InflateException:Binary xml文件行#33:错误膨胀类android.support.v7.widget.RecyclerView原因:java.lang.ClassNotFoundException:未找到类“android.support.v7.widget.RecyclerView“在路上。将构建梯度的依赖项添加到问题。发布代码。请稍候请附上您的代码添加更多详细信息和代码,布局和渐变发布xml代码问题似乎出在项目的依赖项中,因为它在类路径中找不到RecyclerView:android.view.InflateException:Binary xml文件行#33:错误膨胀类android.support.v7.widget.RecyclerView原因:java.lang.ClassNotFoundException:未找到类“android.support.v7.widget.RecyclerView“在路上。将构建梯度的依赖项添加到问题。发布代码。请wait@AngeloDayao请记住,对回答您问题的贡献表示感谢的公认方式是单击答案左侧的复选标记。这将让其他人知道问题已经得到了回答,并将网站积分奖励给努力撰写该问题的人:-)无需为androidX中的RecyclerView、CardVIew添加依赖项只需添加此依赖项
implementation'com.google.android.material:material:1.0.0'
@Angelodayo请记住,对回答您问题的贡献表示感谢的公认方式是单击答案左侧的复选标记。这将让其他人知道问题已经得到了回答,并将网站积分奖励给努力撰写该问题的人:-)无需在androidX中为RecyclerView、CardVIew添加依赖项,只需添加此依赖项
implementation'com.google.android.material:material:1.0.0'
<?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>
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'
}