【问题标题】:ClassCastException for android.widget.LinearLayoutandroid.widget.LinearLayout 的 ClassCastException
【发布时间】:2012-07-12 04:13:10
【问题描述】:

我已经为我的一项活动设计了布局,但出现了奇怪的错误。这是一个简单的活动,实际上它是包含图像按钮的主菜单活动。

"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

【问题讨论】:

  • 一次 只需清理您的项目并运行。并且,重新启动您的工作区并尝试一下
  • 将相对布局更改为线性布局表格布局仅适用于此,因为您也没有完美设置xml文件的属性
  • @user1155908 您的 xml 代码没有错误,只需清理您的项目并再次运行。
  • 转到这个链接,它可能对你有帮助 stackoverflow.com/questions/4393775/…>
  • @user1155908 你的布局和xml很好。再次尝试重建

标签: android android-linearlayout


【解决方案1】:

在您的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);

【讨论】:

    【解决方案2】:

    您能粘贴完整的 logcat 错误吗?因为我认为您正在尝试在您的 Java 代码中使用您的 xml 文件中定义的线性布局之一(可能是最后两个线性布局之一)并且您没有正确声明它们。

    并尝试清理您的项目并运行一次。有时这会发生在 Android 中,我也注意到了。

    【讨论】:

      【解决方案3】:

      这个错误的出现主要是由于错误的类型转换。例如下面的代码: 同样的错误在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();
                      }
                  });
      

      【讨论】:

        【解决方案4】:

        这听起来你需要做的就是一个干净的项目。当您第一次放置scrollView 时,有时会发生这种情况。不过,一扫而光应该可以解决问题。

        【讨论】:

          猜你喜欢
          • 2023-04-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-06-07
          • 2020-08-22
          相关资源
          最近更新 更多