ListView 自定义item布局 android.widget.LinearLayout$LayoutParams

来源:互联网 发布:gta5需网络才能启动 编辑:程序博客网 时间:2024/05/16 14:52

[xhtml] view plaincopyprint?
  1. 05-09 10:51:21.522: ERROR/AndroidRuntime(4425): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams  
 


[java] view plaincopyprint?
  1.     setListAdapter(new BaseAdapter() {  
  2.               
  3.             @Override  
  4.             public View getView(int position, View convertView, ViewGroup parent) {  
  5.                 LinearLayout lnLayout = new LinearLayout(ListActivityDemo02.this);  
  6. //              lnLayout.setLayoutParams(new LinearLayout.LayoutParams(  
  7. //                      ViewGroup.LayoutParams.FILL_PARENT, android.R.attr.listPreferredItemHeight));  
  8.                 lnLayout.setOrientation(LinearLayout.VERTICAL);  
  9.                 ViewGroup.LayoutParams tvLayout = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,  
  10.                             ViewGroup.LayoutParams.WRAP_CONTENT);  
  11.                   
  12.                 TextView tv = new TextView(ListActivityDemo02.this);  
  13.                 tv.setText("hello world");  
  14.                 tv.setLayoutParams(tvLayout);  
  15.                 lnLayout.addView(tv);  
  16.                   
  17.                 TextView tv2 = new TextView(ListActivityDemo02.this);  
  18.                 tv2.setText("hello world111111111");  
  19.                 tv2.setLayoutParams(tvLayout);  
  20.                 lnLayout.addView(tv2);  
  21.                   
  22.                   
  23.                 return lnLayout;  
  24.             }  
  25.               
  26.             @Override  
  27.             public long getItemId(int position) {  
  28.                 return 1;  
  29.             }  
  30.               
  31.             @Override  
  32.             public Object getItem(int position) {  
  33.                 return null;  
  34.             }  
  35.               
  36.             @Override  
  37.             public int getCount() {  
  38.                 return 1;  
  39.             }  
  40.         });  
 


如果对外层的 Layout设置属性的话, 

lnLayout.setLayoutParams(new LinearLayout.LayoutParams(

      ViewGroup.LayoutParams.FILL_PARENT, android.R.attr.listPreferredItemHeight));


则会报错。。。如上的错误信息,,具体的如下

解决办法就是取消对其中的设置


[xhtml] view plaincopyprint?
  1. 05-05 18:42:22.331: ERROR/AndroidRuntime(5833): ERROR: thread attach failed  
  2. 05-05 18:42:23.581: ERROR/AndroidRuntime(5848): Uncaught handler: thread main exiting due to uncaught exception  
  3. 05-05 18:42:23.601: ERROR/AndroidRuntime(5848): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams  
  4. 05-05 18:42:23.601: ERROR/AndroidRuntime(5848):     at android.widget.ListView.setupChild(ListView.java:1702)  
  5. 05-05 18:42:23.601: ERROR/AndroidRuntime(5848):     at android.widget.ListView.makeAndAddView(ListView.java:1671)  
  6. 05-05 18:42:23.601: ERROR/AndroidRuntime(5848):     at android.widget.ListView.fillDown(ListView.java:637)  
  7. 05-05 18:42:23.601: ERROR/AndroidRuntime(5848):     at android.widget.ListView.fillFromTop(ListView.java:694)  
  8. 05-05 18:42:23.601: ERROR/AndroidRuntime(5848):     at android.widget.ListView.layoutChildren(ListView.java:1521)  
  9. 05-05 18:42:23.601: ERROR/AndroidRuntime(5848):     at android.widget.AbsListView.onLayout(AbsListView.java:1113)  
  10. 05-05 18:42:23.601: ERROR/AndroidRuntime(5848):     at android.view.View.layout(View.java:6834)  
  11. 05-05 18:42:23.601: ERROR/AndroidRuntime(5848):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)  
  12. 05-05 18:42:23.601: ERROR/AndroidRuntime(5848):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)  
  13. 05-05 18:42:23.601: ERROR/AndroidRuntime(5848):     at android.widget.LinearLayout.onLayout(LinearLayout.java:918)  
  14. 05-05 18:42:23.601: ERROR/AndroidRuntime(5848):     at android.view.View.layout(View.java:6834)  
  15. 05-05 18:42:23.601: ERROR/AndroidRuntime(5848):     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:900)  
  16. 05-05 18:42:23.601: ERROR/AndroidRuntime(5848):     at android.view.View.layout(View.java:6834)  
  17. 05-05 18:42:23.601: ERROR/AndroidRuntime(5848):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)  
  18. 05-05 18:42:23.601: ERROR/AndroidRuntime(5848):     at android.view.View.layout(View.java:6834)  
  19. 05-05 18:42:23.601: ERROR/AndroidRuntime(5848):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)  
  20. 05-05 18:42:23.601: ERROR/AndroidRuntime(5848):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)  
  21. 05-05 18:42:23.601: ERROR/AndroidRuntime(5848):     at android.widget.LinearLayout.onLayout(LinearLayout.java:918)  
  22. 05-05 18:42:23.601: ERROR/AndroidRuntime(5848):     at android.view.View.layout(View.java:6834)  
  23. 05-05 18:42:23.601: ERROR/AndroidRuntime(5848):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)  
  24. 05-05 18:42:23.601: ERROR/AndroidRuntime(5848):     at android.view.View.layout(View.java:6834)  
  25. 05-05 18:42:23.601: ERROR/AndroidRuntime(5848):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1016)  
  26. 05-05 18:42:23.601: ERROR/AndroidRuntime(5848):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1666)  
  27. 05-05 18:42:23.601: ERROR/AndroidRuntime(5848):     at android.os.Handler.dispatchMessage(Handler.java:99)  
  28. 05-05 18:42:23.601: ERROR/AndroidRuntime(5848):     at android.os.Looper.loop(Looper.java:123)  
  29. 05-05 18:42:23.601: ERROR/AndroidRuntime(5848):     at android.app.ActivityThread.main(ActivityThread.java:4603)  
  30. 05-05 18:42:23.601: ERROR/AndroidRuntime(5848):     at java.lang.reflect.Method.invokeNative(Native Method)  
  31. 05-05 18:42:23.601: ERROR/AndroidRuntime(5848):     at java.lang.reflect.Method.invoke(Method.java:521)  
  32. 05-05 18:42:23.601: ERROR/AndroidRuntime(5848):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)  
  33. 05-05 18:42:23.601: ERROR/AndroidRuntime(5848):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)  
  34. 05-05 18:42:23.601: ERROR/AndroidRuntime(5848):     at dalvik.system.NativeStart.main(Native Method)  
  35. 05-05 18:42:25.341: ERROR/PackageInstallationReceiver(146): Remove /data/local/tmp/com.mmb.shop.apk Fail!  
  36. 05-05 18:42:34.381: ERROR/ActivityManager(88): fail to set top app changed!  
 



看到http://hi.baidu.com/ljlkings/blog/item/86ed2401029b4efd08fa9303.html


受到了一点启发 :


Caused by: java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams
2010-12-21 17:38

 

These supply parameters to the parent of this view specifying how it should be arranged. There are many subclasses of ViewGroup.LayoutParams, and these correspond to the different subclasses of ViewGroup that are responsible for arranging their children.

So basically, if you are adding a view to another, you MUST set the LayoutParams of the view to the LayoutParams type that the parent uses, or you will get a runtime error.

 

 

就是說你要設置此layoutparameters須對父對象佈局設定,最上層的layout不需設定此項。

 

原创粉丝点击