java - androidx.cardview.widget.CardView 无法转换为 androidx.recyclerview.widget.RecyclerView

标签 java android android-recyclerview

我正在制作一个项目,但是当我编译我的项目并在 Android Studio 上启动应用程序时,我的应用程序崩溃了。

在我的 Logcat 错误中

Process: com.passionategeekz.learnC, PID: 5854
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.passionategeekz.learnC/com.passionategeekz.learnC.card}: java.lang.ClassCastException: androidx.cardview.widget.CardView cannot be cast to androidx.recyclerview.widget.RecyclerView
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2895)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1616)
    at android.os.Handler.dispatchMessage(Handler.java:106)``

我的卡.java

  package com.passionategeekz.learnC;

    import android.os.Bundle;

    import androidx.appcompat.app.AppCompatActivity;
    import androidx.recyclerview.widget.DefaultItemAnimator;
    import androidx.recyclerview.widget.LinearLayoutManager;
    import androidx.recyclerview.widget.OrientationHelper;
    import androidx.recyclerview.widget.RecyclerView;

    import java.util.ArrayList;


    public class card extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.card);


            ArrayList<model> list= new ArrayList();
            list.add(new model(model.TEXT_TYPE,"xxx",0));
            list.add(new model(model.IMAGE_TYPE,"xx",R.drawable.wtc));
            list.add(new model(model.AUDIO_TYPE,"xx",R.raw.sound));
            list.add(new model(model.IMAGE_TYPE,"xx",R.drawable.snow));

            MultiViewTypeAdapter adapter = new MultiViewTypeAdapter(list,this);
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this,OrientationHelper.VERTICAL,false);

            RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
            mRecyclerView.setLayoutManager(linearLayoutManager);
            mRecyclerView.setItemAnimator(new DefaultItemAnimator());
            mRecyclerView.setAdapter(adapter);
        }
    }

我的卡.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:fitsSystemWindows="true"
    tools:context="com.passionategeekz.learnC.MainActivity">


    <androidx.cardview.widget.CardView
        android:id="@+id/recyclerView"
        android:layout_width="350dp"
        android:layout_height="447dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </androidx.cardview.widget.CardView>


</androidx.constraintlayout.widget.ConstraintLayout>

我的 Gradle :

implementation "androidx.cardview:cardview:1.0.0"
implementation 'androidx.recyclerview:recyclerview:1.1.0'

最佳答案

问题出在这一行:

RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);

ID为“recyclerView”的 View 是一个CardView,您试图将其转换为RecyclerView。在 xml 中将其类型更改为 RecyclerView 而不是 CardView。

关于java - androidx.cardview.widget.CardView 无法转换为 androidx.recyclerview.widget.RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59045599/

相关文章:

java - 添加 Google Play 服务导致错误 Conversion to Dalvik Format failed with error 1

android - 在Android上如何统计每日、每周、每月的3G、WiFi上网流量?

java - Android Paging Library如何知道加载更多数据?

android - 使用 Animation 使 ItemDecoration 移动

android - RecyclerView 适配器返回零子

java - Selenium 中 WebElements 与 WebElement 的区别

Java String自动赋空值

Java 6 内存使用过多

android - 使用 HttpUriRequest 与 HttpURLConnection 的优缺点是什么?

android - 应用程序图标未显示在设备上的程序列表中