简体   繁体   中英

ClassCastException for android.widget.LinearLayout

I have designed the layout for one of my activities but i am getting strange error . This is simple activity actually it is main menu activity which contains imagebuttons in it.

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

Here is my layout xml Code

<?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>

In java at first line of this code below i am getting error

 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);

            }
        });

Error is

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

Can you paste your full logcat error? Coz I think you are trying to use one of the LinearLayouts defined in your xml file in your Java code (maybe one of the last two linearlayouts) and you have not declared them properly.

And try cleaning your project and run it once. Sometimes this happens in Android, I've noticed too.

In your com.colony.rpgbluetoothchatlite.Menu_Main class...

find out where you call something like this.. I made the variable button.. but yours should be something else...

What's happening is.. your R.id.ban1_mainmenu is a RelativeLayout ... but you are trying to convert it to a different class... in this case I am converting to Button class.. which will throw the same exception you have...

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

to fix it..

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

This error appears mostly due to wrong type casting. eg below code: same error created trouble for me in listView I'm pasting code:

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();
                }
            });

This sounds like all you need to do is a clean project. This sometimes happens when you first put a scrolView in place. A clean sweep should do the trick, however.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM