Android.widget,textView ne peut pas être lancé pour android.widget de bouton
Je reçois une erreur d'exécution lors du lancement de mon activité, et il dit android.widget de.textview ne peut pas être lancé pour android.widget de.bouton?
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:ignore="HardcodedText" >
<TextView
android:id="@+id/textViewName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="false"
android:layout_alignParentTop="false"
android:text="Name:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textViewLevel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/textViewName"
android:text="Level:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textViewClass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="false"
android:layout_below="@+id/textViewLevel"
android:text="Class:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textViewClass"
android:layout_alignBottom="@+id/textViewClass"
android:layout_centerHorizontal="true"
android:text="Title:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textViewXP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textViewTitle"
android:layout_alignLeft="@+id/textViewTitle"
android:text="XP:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textViewGold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textViewXP"
android:layout_alignParentTop="true"
android:text="Gold:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textViewLineBreak"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textViewClass"
android:text="________________________________________"
android:textAppearance="?android:attr/textAppearanceMedium"
android:visibility="invisible" />
<TextView
android:id="@+id/textViewGoldValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/textViewGold"
android:text="0"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/textViewTitle"
android:text="Stats" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button3"
android:layout_alignParentLeft="true"
android:layout_marginBottom="20dp"
android:layout_toLeftOf="@+id/textViewTitle"
android:text="Equipment" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button2"
android:layout_alignParentLeft="true"
android:layout_alignRight="@+id/button2"
android:layout_marginBottom="22dp"
android:text="Inventory" />
<Button
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_alignLeft="@+id/button4"
android:layout_alignParentRight="true"
android:text="Shop" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/button2"
android:layout_toRightOf="@+id/textViewXP"
android:text="Class Training" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button5"
android:layout_alignParentRight="true"
android:layout_below="@+id/button1"
android:text="Crafting" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textViewLineBreak"
android:text="HP:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textViewStrTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textViewLineBreak"
android:layout_alignLeft="@+id/textViewGoldValue"
android:text="N/A"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/textViewXPValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textViewGoldValue"
android:layout_below="@+id/textViewGoldValue"
android:text="0"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textViewLevelValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textViewClass"
android:layout_toRightOf="@+id/textViewName"
android:text="1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/TextViewStrClass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textViewLineBreak"
android:layout_alignLeft="@+id/textViewLevelValue"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/textViewRace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textViewClass"
android:text="Race:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView4"
android:layout_alignBottom="@+id/textView4"
android:layout_toLeftOf="@+id/TextViewStrClass"
android:text="10"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="@+id/spinnerMonsters"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView4"
android:layout_marginTop="50dp"
android:layout_toLeftOf="@+id/btnBattle"
android:entries="@array/Monsters"
android:prompt="@string/MonsterPrompt" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/spinnerMonsters"
android:layout_alignRight="@+id/textViewXPValue"
android:text="10"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/btnBattle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/spinnerMonsters"
android:text="Battle!" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textViewStrTitle"
android:layout_alignRight="@+id/spinnerMonsters"
android:layout_marginRight="14dp"
android:text="/"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textViewXpNextLevel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textViewLevel"
android:layout_alignBottom="@+id/textViewLevel"
android:layout_alignLeft="@+id/btnBattle"
android:text="100"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textViewBattleResults"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/spinnerMonsters"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/textViewStrRace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView1"
android:layout_toRightOf="@+id/textViewClass"
android:text="l"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/textViewStrAlliance"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/textView2"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/textViewStrTitle"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/textViewAlliance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textViewTitle"
android:layout_toRightOf="@+id/button1"
android:text="Alliance:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textViewAlliance"
android:layout_below="@+id/textViewLineBreak"
android:text="MP:"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
Ce qui se passe ici? J'ai traversé plusieurs fois et ne peut pas trouver quelque chose
Edit:
09-17 18:28:51.485: E/AndroidRuntime(724): FATAL EXCEPTION: main
09-17 18:28:51.485: E/AndroidRuntime(724): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.intent_buttontests/com.example.intent_buttontests.PlayScreen}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
09-17 18:28:51.485: E/AndroidRuntime(724): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
09-17 18:28:51.485: E/AndroidRuntime(724): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
09-17 18:28:51.485: E/AndroidRuntime(724): at android.app.ActivityThread.access$600(ActivityThread.java:123)
09-17 18:28:51.485: E/AndroidRuntime(724): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
09-17 18:28:51.485: E/AndroidRuntime(724): at android.os.Handler.dispatchMessage(Handler.java:99)
09-17 18:28:51.485: E/AndroidRuntime(724): at android.os.Looper.loop(Looper.java:137)
09-17 18:28:51.485: E/AndroidRuntime(724): at android.app.ActivityThread.main(ActivityThread.java:4424)
09-17 18:28:51.485: E/AndroidRuntime(724): at java.lang.reflect.Method.invokeNative(Native Method)
09-17 18:28:51.485: E/AndroidRuntime(724): at java.lang.reflect.Method.invoke(Method.java:511)
09-17 18:28:51.485: E/AndroidRuntime(724): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-17 18:28:51.485: E/AndroidRuntime(724): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-17 18:28:51.485: E/AndroidRuntime(724): at dalvik.system.NativeStart.main(Native Method)
09-17 18:28:51.485: E/AndroidRuntime(724): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
09-17 18:28:51.485: E/AndroidRuntime(724): at com.example.intent_buttontests.PlayScreen.onCreate(PlayScreen.java:110)
09-17 18:28:51.485: E/AndroidRuntime(724): at android.app.Activity.performCreate(Activity.java:4465)
09-17 18:28:51.485: E/AndroidRuntime(724): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-17 18:28:51.485: E/AndroidRuntime(724): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
09-17 18:28:51.485: E/AndroidRuntime(724): ... 11 more
- Vous avez probablement fait
(Button) findViewById(R.id.actuallyATextView)
quelque part. - Besoin de consulter votre code d'Activité qui crée de la vue, mais pensez que vous êtes tentant de jeter un TextView à un Bouton, par exemple: TextView textViewName = (Button) findViewById(R. id.textViewName); serait la cause d'une telle erreur.
- Le code que vous avez posté l'air bien... Merci de poster le LogCat erreurs. Peut-être que l'erreur est dans un autre fichier?
- J'ai ajouté le logcat. Mais je n'ai aucune idée de ce qui se passe
- Salut cette erreur est facile, mais ne peut pas understaning
- Type de Moulage comme le bouton textview ,image
- Le bouton b=findviewbyid(R. id.btn);
- textview à inclure dans le fichier xml
InformationsquelleAutor Adariel Lzinski | 2012-09-17
Vous devez vous connecter pour publier un commentaire.
C'est l'information la plus importante dans votre LogCat fichier:
Vous lisez l'erreur correctement, il est un
ClassCastException
. Les lignes ci-dessousCaused by...
vous dire où l'erreur a été lancé, qui est enPlayScreen.onCreate()
sur la ligne 110. Du mieux que je peux dire de la ligne 110:Mais cette ligne est fine et le XML pour
btnBattle
a l'air bien aussi...J'ai couru votre Activité avec votre mise en page moi-même et de ne pas faire d'erreurs. Avez-vous nettoyé votre projet? Souvent, cela permettra de supprimer les "fantômes" des erreurs.
(Dans Eclipse, Projet -> Propre...)
- Je faire une suggestion, vous avez beaucoup de Boutons qui font des tâches similaires. Vous pouvez effectuer les mêmes actions avec beaucoup moins de code si vous utilisez le XML
onClick
attribut. D'abord créer une méthode (que l'on appelleralaunchClick()
) dans votre Activité comme suit:Et ajouter l'attribut
android:onClick
pour chaque Bouton que vous devriez avoir ce comportement dansplay_screen.xml
:Espère que ça aide!
implements OnClickListener
parce que ce n'est pas nécessaire.)Projet -> Nettoyer peut vous aider.
vous êtes en train de jeter
TextView
àButton
.pour
TextView
ce faire :pour
Button
:Dans Eclipse, allez dans Projet > Nettoyer, choisissez votre projet, et c'est tout... que Votre application s'exécute normalement...
Je suis à l'aide d'un RelativeLayout avec 3 textViews et 2 boutons. Je reçois le même bizarre d'erreur lorsque je bouge le placement de la textViews à l'aide de l'interface graphique de l'éditeur. Si je les renvoyer d'où ils étaient initialement placés, l'erreur disparaît.
Je peux me débarrasser de l'erreur par:
Cela semble être un bug depuis le code XML est légitime, mais n'a pas construit au moment de l'exécution correctement.
Espère que cela fonctionne pour vous.
Eclipse Java EE IDE pour les Développeurs Web.
Version: Indigo Service Release 2
Build id: 20120216-1857
Veuillez joindre votre code java pour trouver où est le problème de se produire. Ce problème survient lorsque vous tentez de convertir le textview à bouton.
Ou
il aider 😀
Assurez-vous que toutes
TextView
le domaine du droitTextView
dans votre .fichier java..Comme cela..
Erreur comme Android.widget,textView ne peut pas être lancé pour android.widget de bouton relevé, car peu de temps que nous avons mentionné
Button
au lieu deTextView
.Si tout est ok, puis Nettoyez votre projet et de rafraîchissement.. Cela a fonctionné pour moi..
Je viens de rencontré le même problème. Cependant, j'ai décidé d'utiliser seulement "Voir" sans remettre. Il fonctionne à la perfection. "setOnClickListener" fonctionne de la même pour notre cas.
par exemple
View button = v.findViewById(button_id);
button.setOnClickListener(...)
Dans le cas où les opérations de nettoyage ne fonctionne pas. La cause peut être lié à utiliser te même ID sur plus d'un point.
Assurez-vous que lors de l'attribution des Identifiants via la mise en page, ou par programmation, qui sont pas dupliqué.
J'ai été confronté à ce problème lors de la assinging faible numéros ID (probablement pas une bonne pratique), comme l'ID de l'endroit où la collision avec un bouton radio coché ID.
Acclamations