27

アクティビティを起動するときに実行時エラーが発生し続け、android.widget.textview を android.widget.button にキャストできないと表示されますか?

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>

ここで何が起きてるの?何回か行ったけど何も見つからない

編集:

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
4

11 に答える 11

90

logcat を追加しました。しかし、私は何が起こっているのか分かりません

これは、LogCat ファイル内の重要な情報です。

Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
  at com.example.intent_buttontests.PlayScreen.onCreate(PlayScreen.java:110)

エラーを正しく読んでくださいClassCastException。以下の行Caused by...は、110 行目でエラーがスローされた場所を示していますPlayScreen.onCreate()。110 行目は次のとおりです。

Button btnBattle = (Button) findViewById(R.id.btnBattle);

しかし、この行は問題なく、XML も問題ないようにbtnBattle見えます...

自分のレイアウトでアクティビティを実行しましたが、エラーは発生しませんでした。プロジェクトをクリーンアップしましたか? 多くの場合、これによりこれらの「ファントム」エラーが解消されます。(Eclipse では、プロジェクト->クリーン... )


1 つの提案があります。同様のタスクを実行するボタンがたくさんあります。onClickXML属性を使用すると、はるかに少ないコードで同じアクションを実行できます。launchClick()まず、次のように Activity にメソッドを作成します (それを呼び出します)。

public void launchClick(View v) {
    Intent intent;

    switch(v.getId()) {
    case R.id.button1:
        intent = new Intent(PlayScreen.this, Inventory.class);
        break;

    case R.id.button2:
        intent = new Intent(PlayScreen.this, Equipment.class);
        break;

    // etc, etc
    }
    startActivityForResult(intent, 0);
};

android:onClickそして、この動作が必要なすべてのボタンに属性を追加しますplay_screen.xml

<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:onClick="launchClick"
    android:text="Stats" />

それが役立つことを願っています!

于 2012-09-17T21:20:19.340 に答える
28

Project -> Cleanが役に立ちます。

于 2013-07-07T12:03:31.380 に答える
4

にキャストしようとしTextViewていButtonます。

これをTextView行うために:

TextView tv = (TextView)findviewById(R.id.your textviewid present in xml layout file);

のためにButton

Button btn1 = (Button)findviewById(R.id.your buttonid present in xml layout file);
于 2012-09-17T19:18:09.973 に答える
1

3 つの textView と 2 つのボタンを持つ RelativeLayout を使用しています。グラフィカル インターフェイス エディターを使用して textViews の配置を移動すると、同じ奇妙なエラーが発生します。それらを元の場所に戻すと、エラーはなくなります。

次の方法でエラーを取り除くことができます。

  1. 問題のある XML ファイルからすべてのデータをクリップボードに切り取ります。
  2. プロジェクトを保存します。
  3. プロジェクトを消去します (XML ファイルのエラーを参照してください)
  4. XML コードを貼り付けて保存します
  5. 掃除
  6. 走る

XML コードは正当ですが、実行時に正しくビルドされないため、これはバグのようです。

それがうまくいくことを願っています。Web 開発者向けの Eclipse Java EE IDE。

バージョン: Indigo Service Release 2 ビルド ID: 20120216-1857

于 2013-09-10T21:33:39.580 に答える
0

同じ問題が発生しました。ただし、キャストせずに「ビュー」のみを使用することにしました。それは完全に機能します。「setOnClickListener」は、この場合と同じように機能します。

例えば View button = v.findViewById(button_id); button.setOnClickListener(...)

于 2016-10-19T04:16:44.677 に答える
0
  1. プロジェクト > クリーン

または

  1. アイテムIDの変更

それは役立ちます:D

于 2014-10-18T21:13:40.720 に答える
0

私は同様の問題を抱えていました.TextViewの代わりにImageViewを使用していました.アクティビティを確認するために戻ったとき、すでに変数をボタンとして宣言していることを発見し、変数をfindViewById(R.id. ivrest)、ここで ivRest は ImageView を参照するため、特定の型で変数を宣言する場合は、変数を再割り当てするときに同じ型である必要があることを確認してください

于 2020-04-24T17:22:38.787 に答える
0

問題が発生している場所を見つけるために、Java コードを添付してください。この問題は、テキストビューをボタンに変換しようとしているときに発生します。

于 2012-09-17T19:06:10.157 に答える