繁体   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

当我尝试执行代码并且应用程序也终止时出现以下错误。 (由: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

<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 不能转换为 android.widget.Button)

在您的布局中, roll_button是一个TextView而在您的活动中,您尝试将findViewById作为Button这就是您收到ClassCastException的原因

第一个解决方案是

用这个

val rollButton: TextView = findViewById(R.id.roll_button)

而不是这个

val rollButton: Button = findViewById(R.id.roll_button)

第二个解决方案是

roll_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>

奖金

也读这个

无需在 kotlin 中findViewById

您正在尝试将 TextView 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:id="@+id/roll_button"
android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="@string/roll"
    />

你有问题在线

val rollButton: Button = findViewById(R.id.roll_button)

您试图将文本视图转换为按钮,因此您必须使用如下所示

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.

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