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

前端之家收集整理的这篇文章主要介绍了java.lang.ClassCastException:android.widget.LinearLayout不能被转换为android.widget.ListView前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
不知道为什么我在下面的代码中得到一个ClassCastException.将ListActivity更改为活动可以纠正错误,但是我想要ListActivity,因为这是我正在尝试构建的更大代码的一部分.任何帮助将不胜感激.谢谢

MainActivity.java

  1. package com.example.debug;
  2.  
  3. import android.app.ListActivity;
  4. import android.os.Bundle;
  5. import android.view.Menu;
  6. import android.view.MenuItem;
  7.  
  8.  
  9. public class MainActivity extends ListActivity {
  10.  
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_main);
  15. }
  16.  
  17.  
  18. @Override
  19. public boolean onCreateOptionsMenu(Menu menu) {
  20. // Inflate the menu; this adds items to the action bar if it is present.
  21. getMenuInflater().inflate(R.menu.main,menu);
  22. return true;
  23. }
  24.  
  25. @Override
  26. public boolean onOptionsItemSelected(MenuItem item) {
  27. // Handle action bar item clicks here. The action bar will
  28. // automatically handle clicks on the Home/Up button,so long
  29. // as you specify a parent activity in AndroidManifest.xml.
  30. int id = item.getItemId();
  31. if (id == R.id.action_settings) {
  32. return true;
  33. }
  34. return super.onOptionsItemSelected(item);
  35. }
  36. }

activity_main.xml中:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:id="@android:id/list"
  4. android:layout_width="fill_parent"
  5. android:layout_height="wrap_content"
  6. android:orientation="horizontal" >
  7.  
  8. <LinearLayout
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:orientation="vertical" >
  12. <!-- Name Label -->
  13. <TextView
  14. android:id="@+id/vehicleType"
  15. android:layout_width="fill_parent"
  16. android:layout_height="wrap_content"
  17. android:textColor="#43bd00"
  18. android:textSize="16sp"
  19. android:textStyle="bold"
  20. android:paddingTop="6dip"
  21. android:text="Mobile1"
  22. android:paddingBottom="2dip" />
  23. <!-- Description Label -->
  24. <TextView
  25. android:id="@+id/vehicleColor"
  26. android:layout_width="fill_parent"
  27. android:layout_height="wrap_content"
  28. android:textColor="#acacac"
  29. android:text="Mobile2"
  30. android:paddingBottom="2dip" />
  31.  
  32.  
  33. <LinearLayout
  34. android:layout_width="fill_parent"
  35. android:layout_height="wrap_content"
  36. android:orientation="horizontal" >
  37. <!-- Cost Label -->
  38. <TextView
  39. android:id="@+id/fuel"
  40. android:layout_width="wrap_content"
  41. android:layout_height="wrap_content"
  42. android:textColor="#000000"
  43. android:gravity="left"
  44. android:textStyle="bold"
  45. android:text="Mobile3" />
  46. <!-- Price Label -->
  47. <TextView
  48. android:id="@+id/treadType"
  49. android:layout_width="wrap_content"
  50. android:layout_height="wrap_content"
  51. android:textColor="#acacac"
  52. android:text="Mobile4"
  53. android:paddingBottom="2dip" />
  54. </LinearLayout>
  55. </LinearLayout>
  56. <ListView
  57. android:id="@android:id/list"
  58. android:layout_width="match_parent"
  59. android:layout_height="wrap_content" >
  60. </ListView>
  61. </LinearLayout>

堆栈跟踪

  1. FATAL EXCEPTION: main
  2. Process: com.example.debug,PID: 1370
  3. 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
  4. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
  5. at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
  6. at android.app.ActivityThread.access$800(ActivityThread.java:135)
  7. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
  8. at android.os.Handler.dispatchMessage(Handler.java:102)
  9. at android.os.Looper.loop(Looper.java:136)
  10. at android.app.ActivityThread.main(ActivityThread.java:5017)
  11. at java.lang.reflect.Method.invokeNative(Native Method)
  12. at java.lang.reflect.Method.invoke(Method.java:515)
  13. at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
  14. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
  15. at dalvik.system.NativeStart.main(Native Method)
  16. Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.ListView
  17. at android.app.ListActivity.onContentChanged(ListActivity.java:241)
  18. at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:293)
  19. at android.app.Activity.setContentView(Activity.java:1929)
  20. at com.example.debug.MainActivity.onCreate(MainActivity.java:14)
  21. at android.app.Activity.performCreate(Activity.java:5231)
  22. at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
  23. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
  24. ... 11 more

解决方法

你的问题是你的布局的根源
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:id="@android:id/list"
  4. android:layout_width="fill_parent"
  5. android:layout_height="wrap_content"
  6. android:orientation="horizontal" >

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

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

猜你在找的Android相关文章