0

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>

4 Answers 4

3

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>

Bonus

Also read this

no need to findViewById in kotlin

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

0

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)

Comments

0

Set id to Button in place of TextView

<Button android:id="@+id/roll_button"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.