gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 19:55:10 30 4
gpt4 key购买 nike

我正在制作一个项目,但是当我编译我的项目并在 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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com