简体   繁体   中英

how to fix aused by: java.lang.ClassCastException: androidx.appcompat.widget.AppCompatTextView cannot be cast to android.widget.Button

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>

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

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

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)

将 id 设置为 Button 代替 TextView

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM