java.lang.ClassCastException: android.widget.LinearLayout 不能转换为 android.widget.ListView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28062894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 12:57:10  来源:igfitidea点击:

java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.ListView

javaandroidlistview

提问by Jagatjeet

Not sure why I am getting a ClassCastException in the code below. Changing ListActivity to Activity corrects the error but I want ListActivity as this is a part of the bigger code I am trying to build. Any help will be appreciated. Thanks

不知道为什么我在下面的代码中收到 ClassCastException。将 ListActivity 更改为 Activity 可以纠正错误,但我想要 ListActivity,因为这是我尝试构建的更大代码的一部分。任何帮助将不胜感激。谢谢

MainActivity.java

主活动.java

package com.example.debug;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

activity_main.xml:

活动_main.xml:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

    <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    <!-- Name Label -->
    <TextView 
     android:id="@+id/vehicleType"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:textColor="#43bd00"
     android:textSize="16sp"
     android:textStyle="bold"
     android:paddingTop="6dip"
     android:text="Mobile1"
     android:paddingBottom="2dip" />
    <!-- Description Label -->
    <TextView 
     android:id="@+id/vehicleColor"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:textColor="#acacac"
     android:text="Mobile2"
     android:paddingBottom="2dip" />


    <LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <!-- Cost Label -->
    <TextView 
     android:id="@+id/fuel"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:textColor="#000000"
     android:gravity="left"
     android:textStyle="bold"
     android:text="Mobile3" />
    <!-- Price Label -->
    <TextView 
     android:id="@+id/treadType"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:textColor="#acacac"
     android:text="Mobile4" 
     android:paddingBottom="2dip" />
    </LinearLayout> 
    </LinearLayout> 
    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>
</LinearLayout>

Stacktrace

堆栈跟踪

FATAL EXCEPTION: main
Process: com.example.debug, PID: 1370
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.debug/com.example.debug.MainActivity}: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.ListView
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
    at android.app.ActivityThread.access0(ActivityThread.java:135)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5017)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.ListView
    at android.app.ListActivity.onContentChanged(ListActivity.java:241)
    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:293)
    at android.app.Activity.setContentView(Activity.java:1929)
    at com.example.debug.MainActivity.onCreate(MainActivity.java:14)
    at android.app.Activity.performCreate(Activity.java:5231)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
    ... 11 more

回答by Blackbelt

your problem is with the root of your layout

你的问题是布局的根源

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

you give it the android:id="@android:id/list"the id the android reserves for the ListViewwidget. Change it in something else. E.g

你给它 android:id="@android:id/list"android 为ListView小部件保留的 id 。换个别的吧。例如

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/my_linear_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

回答by Shivam Verma

In a ListActivity android looks for the widget with android:id="@android:id/list"and expects a ListView but in your case it finds the root LinearLayout and tries to cast it to a ListView hence the exception :

在 ListActivity android 中寻找带有android:id="@android:id/list"ListView的小部件, 但在您的情况下,它找到根 LinearLayout 并尝试将其转换为 ListView 因此异常:

Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.ListView

To solve it, you just need to provide a different id as mentioned by Blackbelt.

要解决它,您只需要提供 Blackbelt 提到的不同 id。

回答by Akash

Your linear layout id should be as follows

您的线性布局 ID 应如下所示

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/my_linear_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

回答by Dhiraj Singh

The solution of this error is LinearLayout expects ID attribute.

这个错误的解决方法是 LinearLayout 需要 ID 属性。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_container2"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <include
        android:id="@+id/toolbar_noaction"
        layout="@layout/toolbar"/>

</LinearLayout>

回答by Yuliia Ashomok

Find all usages of problematic id.
In IDEA IDE You can press Ctrl and click on your id for getting all usages.

查找有问题的 id 的所有用法。
在 IDEA IDE 中,您可以按 Ctrl 并单击您的 ID 以获取所有用法。

Note: It must be only one item with this id.

注意:它必须只有一个具有此 id 的项目。

回答by zeusboy

This problem can occur when we have a container in so many case a LinearLayout without id declared, you must put an android:id="@+id/layout_container" in your LinearLayout at the second level

当我们有一个容器时,可能会发生这个问题,在很多情况下,一个 LinearLayout 没有声明 id,你必须在你的 LinearLayout 的第二级放置一个 android:id="@+id/layout_container"