2011年7月22日金曜日

Layout関連のClassCastException

開発環境:
Windows Vista Home
eclipse 3.5(ガリレオ)
ADT_今日の時点で最新の物(バージョン不明)
JavaSDK 1.6.0_24
AndroidSDK_r11
開発・ターゲットVer=Android2.1


テスト環境:
媒体 = エミュレータ
↑SDK ManagerでVer2.1のHVGAで作成


現象:
アプリ起動時にExceptionが発生し起動しない

内容は以下


java.lang.RuntimeException: Unable to start activity ComponentInfo{jp.test/jp.test.CTestActivity}: java.lang.ClassCastException: android.widget.TextView
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)





Caused by: java.lang.ClassCastException: android.widget.TextView
    at jp.test.CTestActivity.onCreate(CTestActivity.java:93)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)


ExceptionはレイアウトファイルのIDを取得しようとすると発生


レイアウトファイルの記述は以下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:id="@+id/linearLayout01"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_weight="1.9">
        <GridView
            android:id="@+id/gridView1"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:numColumns="1">
        </GridView>
    </LinearLayout>

    <View
        android:id="@+id/Separator01"
        android:layout_width="wrap_content"
        android:background="#FFFFFF"
        android:layout_height="1dp"
        android:layout_marginBottom="2dp">
    </View>

    <LinearLayout
        android:id="@+id/linearLayout02"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent">
        <Button
            android:text="注文完了"
            android:id="@+id/btnDone"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <Button
            android:text="キャンセル"
            android:id="@+id/btnCancel"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <Button
            android:text="ダミー"
            android:id="@+id/btnDamy"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>

    <View android:id="@+id/Separator02"
        android:layout_height="1dp"
        android:layout_width="wrap_content"
        android:background="#FFFFFF"
        android:layout_marginBottom="2dp">
    </View>

    <LinearLayout
        android:id="@+id/linearLayout03"
        android:orientation="vertical"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent">
    </LinearLayout>

    <View
        android:id="@+id/Separator03"
        android:layout_width="wrap_content"
        android:background="#FFFFFF"
        android:layout_height="1dp"
        android:layout_marginBottom="2dp">
    </View>

    <ScrollView
        android:id="@+id/svMenu"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_weight="1">
        <TableLayout
            android:id="@+id/tlMenu"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:layout_weight="1">
        </TableLayout>
    </ScrollView>

</LinearLayout>
 

ソースは以下


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mLinearLayout01 = (LinearLayout)findViewById(R.id.linearLayout01);
    mLinearLayout02 = (LinearLayout)findViewById(R.id.linearLayout02);
    mLinearLayout03 = (LinearLayout)findViewById(R.id.linearLayout03);
}

Exceptionは「findViewById(R.id.linearLayout03)」を実行すると発生。

id名が間違っているわけでもない。

レイアウトファイル側は「LinearLayout」として作ってあるのでキャストも「LinearLayout」にしている。
01と02がうまく行っているのに03だけうまくいかない。

エラー行はコメントアウトし、03を分解してみた。


Object aaa = findViewById(R.id.linearLayout03);
mLinearLayout03 = (LinearLayout)aaa;


ワンクッション置いただけ。ステップ実行したがうまくいく。

エラー行と同じ記述で新規にもう一行作る。


//mLinearLayout03 = (LinearLayout)findViewById(R.id.linearLayout03);
Object aaa = findViewById(R.id.linearLayout03);
mLinearLayout03 = (LinearLayout)aaa;
mLinearLayout03 = (LinearLayout)findViewById(R.id.linearLayout03);


うまくいく。

エラー行にごみデータでもあったのか。

eclipseかAndroidのバグなのか。

原因不明。

 
   ↓クリックで、このブログの評価が上がり執筆者が喜びます
にほんブログ村 IT技術ブログ プログラム・プログラマーへ
   にほんブログ村

0 件のコメント: