【问题标题】: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
【发布时间】:2019-06-06 06:33:41
【问题描述】:

当我尝试执行代码并且应用程序也终止时,会发生以下错误。 (原因: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 android xml kotlin


    【解决方案1】:

    java.lang.ClassCastException: androidx.appcompat.widget.AppCompatTextView 无法转换为 android.widget.Button)

    在您的布局中,roll_buttonTextView,而在您的活动中,您尝试将 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

    【讨论】:

      【解决方案2】:

      您正在尝试将 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"
          />
      

      【讨论】:

        【解决方案3】:

        你在网上有问题

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

        您尝试将文本视图转换为按钮,因此您必须像下面这样使用

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

        【讨论】:

          【解决方案4】:

          将 id 设置为 Button 代替 TextView

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2016-06-29
            • 1970-01-01
            • 2017-12-31
            • 2022-01-02
            • 2022-01-18
            • 2020-03-11
            • 1970-01-01
            • 2015-10-01
            相关资源
            最近更新 更多