1

アクティビティの1つにレイアウトを設計しましたが、奇妙なエラーが発生します。これは単純なアクティビティであり、実際にはイメージボタンを含むメインメニューアクティビティです。

"07-12 16:40:22.599: E/AndroidRuntime(1857): Caused by: java.lang.ClassCastException: android.widget.LinearLayout
"

これが私のレイアウトxmlコードです

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="fill_parent" >


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/rpg"
        android:orientation="vertical" >

        <TableLayout
            android:id="@+id/tableLayout1"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center" >

                <TextView android:text="                                 " />
            </TableRow>

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center" >

                <TextView android:text="                                 " />
            </TableRow>

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center" >

                <TextView android:text="                                 " />
            </TableRow>

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center" >

                <TextView android:text="                                 " />
            </TableRow>

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center" >

                <TextView android:text="                                 " />
            </TableRow>

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center" >

                <TextView android:text="                                 " />
            </TableRow>

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center" >

                <TextView android:text="                                 " />
            </TableRow>

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center" >

                <TextView android:text="                                 " />
            </TableRow>

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center" >

                <TextView android:text="                            " />
            </TableRow>

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center" >

                <TextView android:text="                             " />
            </TableRow>

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center" >

                <TextView android:text="                                 " />
            </TableRow>

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center" >

                <ImageButton
                    android:id="@+id/profile_btn"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:src="@drawable/a1" />

                <TextView android:text=" " />

                <ImageButton
                    android:id="@+id/share_btn"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:src="@drawable/a2" />
            </TableRow>

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center" >

                <TextView android:text="                              " />
            </TableRow>

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center" >
            </TableRow>

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center" >

                <TextView android:text="                                 " />
            </TableRow>

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center" >

                <ImageButton
                    android:id="@+id/settings_btn"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:src="@drawable/a3" />

                <TextView android:text="  " />

                <ImageButton
                    android:id="@+id/contacts_btn"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:src="@drawable/a4" />
            </TableRow>

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center" >

                <TextView android:text="                                 " />
            </TableRow>

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center" >

                <ImageButton
                    android:id="@+id/exit_btn"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:src="@drawable/a5" />

                <TextView android:text="  " />

                <ImageButton
                    android:id="@+id/amazon_btn"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:src="@drawable/amazon" />
            </TableRow>
        </TableLayout>

        <LinearLayout
            android:id="@+id/ban2_mainmenu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:background="@drawable/ban_2" >
        </LinearLayout>

        <LinearLayout
            android:id="@+id/ban1_mainmenu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignRight="@+id/ban2_mainmenu"
            android:background="@drawable/ban_1" >
        </LinearLayout>
    </RelativeLayout>

</ScrollView>

以下のこのコードの最初の行のJavaでエラーが発生しています

 ImageButton amazon = (ImageButton) findViewById(R.id.amazon_btn);
        amazon.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                Intent browse = new Intent(Intent.ACTION_VIEW, Uri
                        .parse("http://www.amazon.com/gp/product/B0084FCDKS"));
                startActivity(browse);

            }
        });

エラーは

07-12 16:40:22.599: E/AndroidRuntime(1857): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.colony.rpgbluetoothchatlite/com.colony.rpgbluetoothchatlite.Menu_Main}: java.lang.ClassCastException: android.widget.LinearLayout
07-12 16:40:22.599: E/AndroidRuntime(1857): Caused by: java.lang.ClassCastException: android.widget.LinearLayout
4

4 に答える 4

2

あなたのcom.colony.rpgbluetoothchatlite.Menu_Mainクラスでは...

あなたがこのようなものをどこで呼ぶかを見つけてください..私は可変ボタンを作りました..しかしあなたのものは何か他のものでなければなりません...

何が起こっているのか..あなたR.id.ban1_mainmenuRelativeLayout...しかしあなたはそれを別のクラスに変換しようとしています...この場合私はButtonクラスに変換しています..あなたが持っているのと同じ例外をスローします...

Button btn1 = findViewById(R.id.ban1_mainmenu);
Button btn2 = findViewById(R.id.ban2_mainmenu);

それを修正する。

LinearLayout btn1 = (LinearLayout)findViewById(R.id.ban1_mainmenu);
RelativeLayout btn2 = findViewById(R.id.ban2_mainmenu);
于 2012-07-12T13:17:46.757 に答える
2

logcatエラー全体を貼り付けることができますか?CozあなたはJavaコードのxmlファイルで定義されたLinearLayoutsの1つ(おそらく最後の2つのlinearlayoutsの1つ)を使用しようとしていて、それらを適切に宣言していないと思います。

そして、プロジェクトをクリーンアップして、一度実行してみてください。これはAndroidで発生することがありますが、私も気づきました。

于 2012-07-12T12:07:25.647 に答える
1

このエラーは、主に型キャストが間違っているために発生します。例:以下のコード:同じエラーにより、listView コードを貼り付けているときに問題が発生しました:

List.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parentAdapter,
                        View view, int position, long id) {
                    // The below line of TextView will generate same problem..
                    TextView clickedView = (TextView) view;
                    Toast.makeText(
                            ActivityName.this,
                            "Item with id [" + id + "] - Position ["
                                    + position + "] - Planet ["
                                    /*+ clickedView.getText()*/ + "]",
                            Toast.LENGTH_SHORT).show();
                }
            });
于 2013-03-17T20:03:04.713 に答える
0

これはあなたがする必要があるのはきれいなプロジェクトだけのように聞こえます。これは、最初にscrolViewを配置したときに発生することがあります。ただし、クリーンスイープでうまくいくはずです。

于 2013-10-30T01:37:20.733 に答える