Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了java.lang.ClassCastException:android.widget.LinearLayout不能被转换为android.widget.ListView大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
不知道为什么我在下面的代码中得到一个ClassCastException.将ListActivity更改为活动可以纠正错误,但是我想要ListActivity,因为这是我正在尝试构建的更大代码的一部分.任何帮助将不胜感激.谢谢 @H_762_2@mainActivity.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中:

<LinearLayout xmlns:android="http://scheR_185_11845@as.android.com/apk/res/android"
        xmlns:tools="http://scheR_185_11845@as.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>

堆栈跟踪

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.access$800(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

解决方法

你的问题是你的布局的根源
<LinearLayout xmlns:android="http://scheR_185_11845@as.android.com/apk/res/android"
        xmlns:tools="http://scheR_185_11845@as.android.com/tools"
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

你给它android:id =“@ android:id / list”的机器保留ListView小部件的id.改变它在别的东西.例如

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

大佬总结

以上是大佬教程为你收集整理的java.lang.ClassCastException:android.widget.LinearLayout不能被转换为android.widget.ListView全部内容,希望文章能够帮你解决java.lang.ClassCastException:android.widget.LinearLayout不能被转换为android.widget.ListView所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。