简体   繁体   English

androidx.cardview.widget.CardView 不能转换为 androidx.recyclerview.widget.RecyclerView

[英]androidx.cardview.widget.CardView cannot be cast to androidx.recyclerview.widget.RecyclerView

I am making a project but when I compile my project and launch the app on Android Studio my app crashes.我正在制作一个项目,但是当我编译我的项目并在 Android Studio 上启动应用程序时,我的应用程序崩溃了。

In my Logcat error在我的 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)``

My card.java我的卡.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);
        }
    }

My card.xml我的卡.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>

My Gradle:我的摇篮:

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

The problem is this row:问题是这一行:

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

The view whith id "recyclerView" is a CardView which you are trying to cast to RecyclerView. id 为“recyclerView”的视图是一个 CardView,您正试图将其转换为 RecyclerView。 Change it`s type to RecyclerView instead of CardView in your xml.在 xml 中将其类型更改为 RecyclerView 而不是 CardView。

Replace cardview with this用这个替换 cardview

<androidx.recyclerview.widget.RecyclerView
    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.recyclerview.widget.RecyclerView>

In your xml change在您的 xml 更改中

<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>

TO

<androidx.recyclerview.widget.RecyclerView
    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.recyclerview.widget.RecyclerView>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误 androidx.cardview.widget.CardView 无法转换为 androidx.appcompat.widget.Toolbar - Error androidx.cardview.widget.CardView cannot be cast to androidx.appcompat.widget.Toolbar androidx.appcompat.widget.AppCompatImageButton 无法转换为 androidx.cardview.widget.CardView - androidx.appcompat.widget.AppCompatImageButton cannot be cast to androidx.cardview.widget.CardView android.widget.FrameLayout 不能转换为 androidx.recyclerview.widget.RecyclerView - android.widget.FrameLayout cannot be cast to androidx.recyclerview.widget.RecyclerView RecyclerView.setAdapter(androidx.recyclerview.widget.RecyclerView 适配器适配器 - RecyclerView.setAdapter(androidx.recyclerview.widget.RecyclerView Adapter ADAPTER 这是什么“RecyclerView 没有 LayoutManager androidx.recyclerview.widget.RecyclerView”错误? - What is this "RecyclerView has no LayoutManager androidx.recyclerview.widget.RecyclerView" error? 尝试从 null object 参考上的字段 'android.view.View androidx.recyclerview.widget.RecyclerView$b0.a' 中读取 - Attempt to read from field 'android.view.View androidx.recyclerview.widget.RecyclerView$b0.a' on a null object reference 尝试从字段 'android.view.View androidx.recyclerview.widget.RecyclerView$ViewHolder.itemView' 读取 null object 参考 - Attempt to read from field 'android.view.View androidx.recyclerview.widget.RecyclerView$ViewHolder.itemView' on a null object reference 使用来自 Firebase Firestore 的两个集合的数据使用 CardView 填充 Androidx RecyclerView - Populate Androidx RecyclerView with CardView with data of two collections from Firebase Firestore 无法转换为 androidx.appcompat.widget.Toolbar - cannot be cast to androidx.appcompat.widget.Toolbar 程序类型已经存在:androidx.recyclerview.widget.AdapterHelper$Callback - Program type already present: androidx.recyclerview.widget.AdapterHelper$Callback
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM