0
votes

Comment réparer causé par: java.lang.classcastException: androidx.appcompat.widget.appcomPattextView ne peut pas être jetée à android.widget.button

L'erreur suivante se produit lorsque j'essaie d'exécuter le code et l'application se terminera également. (Causé par: java.lang.classcastException: androidx.appcompat.widget.appcompattextview ne peut pas être jeté sur android.widget.button) xxx

xml xxx


0 commentaires

4 Réponses :


3
votes

Java.lang.classcastException: androidx.appcompat.widget.appcompattextview ne peut pas être jeté sur android.widget.button) p>

Dans votre mise en page Le roll_button code> est un textview code> strong> et dans votre acité, vous essayez de faire Findviewide code> comme BOUTON CODE> C'est pourquoi vous obtenez classcastexception code> p>

première solution est p> blockQuote>

Utilisez ce p> xxx pré>

au lieu de cette p> xxx pré>

deuxième solution est p> BlockQuote>

Attribuer roll_button code> Strong> ID sur votre bouton code> dans votre fichier de mise en page p>

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


0 commentaires

2
votes

Vous essayez de définir TextView ID sur un bouton. Changez votre code de mise en page en ce xxx


0 commentaires

0
votes

Vous avez un problème sur la ligne

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


0 commentaires

0
votes

ID de réglage sur le bouton à la place de TextView

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


0 commentaires