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 でコントロールを検索/検出するのと同じように、どうして間違っているのでしょうか。