簡體   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