0

Android レイアウト クリエーターでコントロールを新しい場所に移動するたびに、コードでコントロールを型キャストできません。

レイアウト XML再配置:

<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
    android:orientation="horizontal"
    tools:context="relax.android.classes.DishesArrayAdapter" >

         <Button
            android:id="@+id/btnDone"
            android:layout_width="75dp"
            android:layout_height="match_parent"
            android:background="@android:drawable/btn_default"
            android:contentDescription="@string/ibtnValueDone" />

        <TextView
            android:id="@+id/tvName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/tvValueName"
            android:textSize="12sp" />

    </LinearLayout>

レイアウト XML再配置:

<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
    android:orientation="horizontal"
    tools:context="relax.android.classes.DishesArrayAdapter" >

         <TextView
            android:id="@+id/tvName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/tvValueName"
            android:textSize="12sp" />

         <Button
            android:id="@+id/btnDone"
            android:layout_width="75dp"
            android:layout_height="match_parent"
            android:background="@android:drawable/btn_default"
            android:contentDescription="@string/ibtnValueDone" />

    </LinearLayout>

コードサンプル:

TextView tvName = (TextView) findViewById(R.id.tvName);
Button btnDone = (Button) findViewById(R.id.btnDone);

デバッグ時にコントロールの位置を変更すると、 がスローされjava.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.TextViewます。

viewレイアウト内の位置ではなく、ID でコントロールを検索/検出するのと同じように、どうして間違っているのでしょうか。

4

2 に答える 2

1

デバッグする前にプロジェクトをクリーンアップします。したがって、このクラスキャスト例外は発生しません。

于 2013-01-01T06:01:16.607 に答える
1

プロジェクトをクリーンアップしてプロジェクトを再ビルドし、デバッグを試みます。したがって、このクラスのキャスト例外は発生しません。

于 2013-01-01T06:16:21.183 に答える