Reputation: 33
The following error occurs when I try to execute the code and app also terminates. (Caused by: java.lang.ClassCastException: androidx.appcompat.widget.AppCompatTextView cannot be cast to android.widget.Button)
package com.tisu.role
import android.annotation.SuppressLint
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
@SuppressLint("WrongViewCast")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val rollButton: Button = findViewById(R.id.roll_button)
rollButton.setOnClickListener {
Toast.makeText(this, "button clicked", Toast.LENGTH_LONG).show()
}
}
}
XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/roll_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/_01"
android:textSize="40sp"
android:layout_gravity="center_horizontal"
/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/roll"
/>
</LinearLayout>
Upvotes: 0
Views: 14200
Reputation: 69699
java.lang.ClassCastException: androidx.appcompat.widget.AppCompatTextView cannot be cast to android.widget.Button)
in your layout the roll_button
is a TextView
and in your acivity your trying doing findViewById
as Button
that's why you are getting ClassCastException
First solution is
Use this
val rollButton: TextView = findViewById(R.id.roll_button)
instead of this
val rollButton: Button = findViewById(R.id.roll_button)
Second solution is
assign roll_button
id to your button
in your layout file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/_01"
android:textSize="40sp"
android:layout_gravity="center_horizontal"
/>
<Button android:layout_width="wrap_content"
android:id="@+id/roll_button"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/roll"
/>
</LinearLayout>
Also read this
no need to findViewById
in kotlin
Upvotes: 3
Reputation: 1482
Set id to Button in place of TextView
<Button android:id="@+id/roll_button"
Upvotes: 0
Reputation: 2538
you have issue on line
val rollButton: Button = findViewById(R.id.roll_button)
You are tried to convert text view as Button so you have to use like below
val rollButton: TextView = findViewById(R.id.roll_button)
Upvotes: 0
Reputation: 1105
you are trying to set TextView id to a button . change your layout code to this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/_01"
android:textSize="40sp"
android:layout_gravity="center_horizontal"
/>
<Button android:id="@+id/roll_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/roll"
/>
Upvotes: 2