kotlin 启动时RecyclerView崩溃

raogr8fs  于 2022-11-30  发布在  Kotlin
关注(0)|答案(2)|浏览(150)

我试图实现一个RecyclerView,调试时它没有显示错误,但在textView上ckick以意图dayone.xml活动(RecyclerView布局)时它崩溃。
我在logcat中得到了这个:

Process: com.example.mozillaevent, PID: 15060
    java.lang.ClassCastException: androidx.cardview.widget.CardView cannot be cast to android.widget.TextView
        at com.example.mozillaevent.Adapter$ViewHolder.<init>(Adapter.kt:37)
        at com.example.mozillaevent.Adapter.onCreateViewHolder(Adapter.kt:16)
        at com.example.mozillaevent.Adapter.onCreateViewHolder(Adapter.kt:11)
        at androidx.recyclerview.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:7078)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6235)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
        at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
        at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
        at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
        at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
        at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
        at android.view.View.layout(View.java:22496)
        at android.view.ViewGroup.layout(ViewGroup.java:6528)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:334)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at android.view.View.layout(View.java:22496)
        at android.view.ViewGroup.layout(ViewGroup.java:6528)
        at androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:536)
        at android.view.View.layout(View.java:22496)
        at android.view.ViewGroup.layout(ViewGroup.java:6528)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:334)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at android.view.View.layout(View.java:22496)
        at android.view.ViewGroup.layout(ViewGroup.java:6528)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1857)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1701)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1610)
        at android.view.View.layout(View.java:22496)
        at android.view.ViewGroup.layout(ViewGroup.java:6528)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:334)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at com.android.internal.policy.DecorView.onLayout(DecorView.java:1146)
        at android.view.View.layout(View.java:22496)
        at android.view.ViewGroup.layout(ViewGroup.java:6528)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3743)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:3207)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2166)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8887)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1280)
        at android.view.Choreographer.doCallbacks(Choreographer.java:1019)
        at android.view.Choreographer.doFrame(Choreographer.java:911)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1248)
        at android.os.Handler.handleCallback(Handler.java:900)
        at android.os.Handler.dispatchMessage(Handler.java:103)
        at android.os.Looper.loop(Looper.java:219)
        at android.app.ActivityThread.main(ActivityThread.java:8668)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1109)

这些是以下类:

  • 类适配器
  • 数据类项目
  • 主活动+屏幕一个类
class Adapter(val items: List<Items>) : RecyclerView.Adapter<Adapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val item = LayoutInflater.from(parent.context).inflate(R.layout.day1, parent, false)
        return ViewHolder(item)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val items = items[position]
        holder.bind(items)
    }

    override fun getItemCount(): Int {
        return (items.size)
    }

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {

        var image: ImageView
        var cardName: TextView
        var day: TextView
        var description: TextView

        init {
            image = view.findViewById<ImageView>(R.id.imageBox)
            description = view.findViewById<TextView>(R.id.descriptionBox)
            day = view.findViewById(R.id.tvDay)
            cardName = view.findViewById(R.id.tvName)
        }

        fun bind(element: Items) {
            image.setImageResource(element.image)
            description.text = element.description
            cardName.text = element.cardName
            day.text = element.day

        }

    }
}

//数据类
第一个
//activity.main

<?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"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/day1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.cardview.widget.CardView
            android:id="@+id/cardOne"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_centerHorizontal="true"
            android:layout_weight="1"
            android:backgroundTint="@color/lightGray"
            android:elevation="30dp"
            android:layout_margin="20dp"
            app:cardCornerRadius="15dp"
            app:cardElevation="15dp">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <TextView
                    android:id="@+id/tvDayOne"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="DAY 1"
                    android:textAlignment="center"
                    android:textSize="100sp"

                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"></TextView>

            </androidx.constraintlayout.widget.ConstraintLayout>
        </androidx.cardview.widget.CardView>

        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_centerHorizontal="true"
            android:layout_weight="1"
            android:backgroundTint="@color/lightGray"
            android:layout_margin="20dp"
            app:cardCornerRadius="15dp"
            app:cardElevation="15dp">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="DAY 2"
                    android:textAlignment="center"
                    android:textSize="100sp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"></TextView>

            </androidx.constraintlayout.widget.ConstraintLayout>
        </androidx.cardview.widget.CardView>

        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_centerHorizontal="true"
            android:layout_weight="1"
            android:layout_margin="20dp"
            android:backgroundTint="@color/Orange"
            app:cardCornerRadius="15dp"
            app:cardElevation="15dp"

>

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="DAY 3"
                    android:textAlignment="center"
                    android:textSize="100sp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"></TextView>

            </androidx.constraintlayout.widget.ConstraintLayout>
        </androidx.cardview.widget.CardView>

        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_centerHorizontal="true"
            android:layout_weight="1"
            android:layout_margin="20dp"
            android:backgroundTint="@color/lightGray"
            app:cardCornerRadius="15dp"
            app:cardElevation="15dp">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="DAY 4"
                    android:textAlignment="center"
                    android:textSize="100sp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"></TextView>

            </androidx.constraintlayout.widget.ConstraintLayout>
        </androidx.cardview.widget.CardView>

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

//day1.xml

<?xml version="1.0" encoding="utf-8"?>

    <androidx.cardview.widget.CardView

    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="300dp"
       android:layout_margin="20dp"
        android:layout_marginBottom="400dp"
        app:cardCornerRadius="15dp"
        app:cardElevation="10dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            tools:layout_editor_absoluteX="9dp"
            tools:layout_editor_absoluteY="239dp">

            <androidx.cardview.widget.CardView
                android:id="@+id/descriptionBox"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:layout_alignParentBottom="true">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <TextView
                        android:id="@+id/tvDes"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/dateLayout"
                        android:layout_alignParentStart="true"
                        android:layout_marginStart="10dp"
                        android:layout_marginLeft="10dp"
                        tools:text="Mozilla description "
                        tools:textColor="@color/black"
                        tools:textSize="15dp">

                    </TextView>

                    <LinearLayout
                        android:id="@+id/dateLayout"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/tvName"
                        android:layout_alignStart="@+id/tvName"
                        android:layout_alignParentLeft="true"
                        android:orientation="horizontal">

                        <TextView
                            android:id="@+id/tvDay"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginStart="10dp"
                            tools:text="Thursday">

                        </TextView>

                    </LinearLayout>

                    <TextView
                        android:id="@+id/tvName"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/descriptionBox"
                        android:layout_marginLeft="10dp"
                        tools:ignore="NotSibling"
                        android:text="Workshop Name"
                        android:textColor="@color/black"
                        android:textSize="25dp">

                    </TextView>

                </RelativeLayout>

            </androidx.cardview.widget.CardView>

            <ImageView
                android:id="@+id/imageBox"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:layout_above="@+id/descriptionBox"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:layout_alignParentRight="true"
                android:scaleType="centerCrop"
                app:srcCompat="@drawable/img">

            </ImageView>

        </RelativeLayout>
    </androidx.cardview.widget.CardView>

//dayone.xml

<?xml version="1.0" encoding="utf-8"?>

    <androidx.recyclerview.widget.RecyclerView
    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:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </androidx.recyclerview.widget.RecyclerView>
q5iwbnjs

q5iwbnjs1#

根据堆栈跟踪,崩溃是由于尝试将CardView强制转换为TextView
您还可以看到它发生在Adapter$ViewHolder类的初始化过程中。
问题很可能是description = view.findViewById<TextView>(R.id.descriptionBox),因为R.id.descriptionBox在布局中被定义为CardView,但您试图将其转换为TextView。(也许您本打算使用R.id.tvDes?)

wpx232ag

wpx232ag2#

您不能将文本设置为cardView,我猜您希望使用tvDes,如下所示:

init {
            image = view.findViewById<ImageView>(R.id.imageBox)
            //Below line changed
            description = view.findViewById<TextView>(R.id.tvDes)
            day = view.findViewById(R.id.tvDay)
            cardName = view.findViewById(R.id.tvName)
        }

相关问题