简体   繁体   English

如何修复使用:java.lang.ClassCastException: androidx.appcompat.widget.AppCompatTextView 不能转换为 android.widget.Button

[英]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) (由:java.lang.ClassCastException: androidx.appcompat.widget.AppCompatTextView 无法转换为 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 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) java.lang.ClassCastException: androidx.appcompat.widget.AppCompatTextView 不能转换为 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在您的布局中, roll_button是一个TextView而在您的活动中,您尝试将findViewById作为Button这就是您收到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 fileroll_button id 分配给布局文件中的button

<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无需在 kotlin 中findViewById

you are trying to set TextView id to a button .您正在尝试将 TextView id 设置为 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"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageButton 不能转换为 android.widget.Button - java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageButton cannot be cast to android.widget.Button java.lang.ClassCastException:android.widget.LinearLayout无法转换为android.widget.Button - java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.Button androidx.appcompat.widget.AppCompatTextView 无法转换为 android.widget.CheckedTextView - androidx.appcompat.widget.AppCompatTextView cannot be cast to android.widget.CheckedTextView java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageView 不能转换为 android.widget.TextView - java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageView cannot be cast to android.widget.TextView java.lang.RuntimeException:无法启动活动组件信息:java.lang.ClassCastException:无法转换为 android.widget.Button - java.lang.RuntimeException: Unable to start activity ComponentInfo: java.lang.ClassCastException: cannot be cast to android.widget.Button 我不断收到错误 java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageView cannot be cast to android.widget.TextView - i keep getting the error java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageView cannot be cast to android.widget.TextView 错误 - java.lang.ClassCastException:com.google.android.material.appbar.AppBarLayout 无法转换为 androidx.appcompat.widget.Toolbar - Error - java.lang.ClassCastException:com.google.android.material.appbar.AppBarLayout cannot be cast to androidx.appcompat.widget.Toolbar java.lang.ClassCastException:androidx.constraintlayout.widget.ConstraintLayout 无法转换为 android.widget.LinearLayout - java.lang.ClassCastException: androidx.constraintlayout.widget.ConstraintLayout cannot be cast to android.widget.LinearLayout 获取错误导致:java.lang.ClassCastException: androidx.viewpager2.widget.ViewPager2 无法转换为 androidx.viewpager.widget.ViewPager - getting error as Caused by: java.lang.ClassCastException: androidx.viewpager2.widget.ViewPager2 cannot be cast to androidx.viewpager.widget.ViewPager Android java.lang.ClassCastException:无法将android.widget.RelativeLayout强制转换为android.widget.EditText - Android java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.EditText
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM