I was trying to implement a RecyclerView, it shows no errors while debugging but it crushes when ckick on the textView to intent to dayone.xml activity (RecyclerView layout).
I get this in 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)
these are the following classes:
- class Adapter
- data class Items
- MainActvity ScreenOne class
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
}
}
}
//data class
data class Items (
val image: Int,
val cardName: String,
val day: String,
val description: String
)
class ScreenOne : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.dayone)
val recyclerview =findViewById<RecyclerView>(R.id.recyclerView)
val elements = listOf(
Items(R.drawable.img, "Android workshop", "tuesday", "Make your first app"
),
Items(R.drawable.img, "Android workshop", "tuesday", "Make your first app"
),
Items(R.drawable.img, "Android workshop", "tuesday", "Make your first app"
),
Items(R.drawable.img, "Android workshop", "tuesday", "Make your first app"
),
Items(R.drawable.img, "Android workshop", "tuesday", "Make your first app"
),
)
recyclerview.apply {
layoutManager = LinearLayoutManager(this@ScreenOne)
}
recyclerview.adapter= Adapter(elements)
}
}
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val intent = Intent(this, ScreenOne::class.java)
val btn = findViewById<TextView>(R.id.tvDayOne)
btn.setOnClickListener {
startActivity(intent)
}
}
}
//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>
CodePudding user response:
From the stack trace, the crash is due to trying to cast a CardView
to a TextView
.
You can also see there that it happens during initialization of Adapter$ViewHolder
class.
The culprit is likely description = view.findViewById<TextView>(R.id.descriptionBox)
since R.id.descriptionBox
is defined as a CardView
in the layout but you're trying to cast it to a TextView
. (maybe you meant to use R.id.tvDes
instead?)
CodePudding user response:
You can not set the text to a cardView, i guess you wanted to use tvDes
like below:
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)
}