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

05-09 10:51:21.522: ERROR/AndroidRuntime(4425): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams  


setListAdapter(new BaseAdapter() { @Override public View getView(int position, View convertView, ViewGroup parent) { LinearLayout lnLayout = new LinearLayout(ListActivityDemo02.this); // lnLayout.setLayoutParams(new LinearLayout.LayoutParams( // ViewGroup.LayoutParams.FILL_PARENT, android.R.attr.listPreferredItemHeight)); lnLayout.setOrientation(LinearLayout.VERTICAL); ViewGroup.LayoutParams tvLayout = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); TextView tv = new TextView(ListActivityDemo02.this); tv.setText("hello world"); tv.setLayoutParams(tvLayout); lnLayout.addView(tv); TextView tv2 = new TextView(ListActivityDemo02.this); tv2.setText("hello world111111111"); tv2.setLayoutParams(tvLayout); lnLayout.addView(tv2); return lnLayout; } @Override public long getItemId(int position) { return 1; } @Override public Object getItem(int position) { return null; } @Override public int getCount() { return 1; } }); 


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

lnLayout.setLayoutParams(new LinearLayout.LayoutParams(

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


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

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


05-05 18:42:22.331: ERROR/AndroidRuntime(5833): ERROR: thread attach failed 05-05 18:42:23.581: ERROR/AndroidRuntime(5848): Uncaught handler: thread main exiting due to uncaught exception 05-05 18:42:23.601: ERROR/AndroidRuntime(5848): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams 05-05 18:42:23.601: ERROR/AndroidRuntime(5848): at android.widget.ListView.setupChild(ListView.java:1702) 05-05 18:42:23.601: ERROR/AndroidRuntime(5848): at android.widget.ListView.makeAndAddView(ListView.java:1671) 05-05 18:42:23.601: ERROR/AndroidRuntime(5848): at android.widget.ListView.fillDown(ListView.java:637) 05-05 18:42:23.601: ERROR/AndroidRuntime(5848): at android.widget.ListView.fillFromTop(ListView.java:694) 05-05 18:42:23.601: ERROR/AndroidRuntime(5848): at android.widget.ListView.layoutChildren(ListView.java:1521) 05-05 18:42:23.601: ERROR/AndroidRuntime(5848): at android.widget.AbsListView.onLayout(AbsListView.java:1113) 05-05 18:42:23.601: ERROR/AndroidRuntime(5848): at android.view.View.layout(View.java:6834) 05-05 18:42:23.601: ERROR/AndroidRuntime(5848): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119) 05-05 18:42:23.601: ERROR/AndroidRuntime(5848): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998) 05-05 18:42:23.601: ERROR/AndroidRuntime(5848): at android.widget.LinearLayout.onLayout(LinearLayout.java:918) 05-05 18:42:23.601: ERROR/AndroidRuntime(5848): at android.view.View.layout(View.java:6834) 05-05 18:42:23.601: ERROR/AndroidRuntime(5848): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:900) 05-05 18:42:23.601: ERROR/AndroidRuntime(5848): at android.view.View.layout(View.java:6834) 05-05 18:42:23.601: ERROR/AndroidRuntime(5848): at android.widget.FrameLayout.onLayout(FrameLayout.java:333) 05-05 18:42:23.601: ERROR/AndroidRuntime(5848): at android.view.View.layout(View.java:6834) 05-05 18:42:23.601: ERROR/AndroidRuntime(5848): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119) 05-05 18:42:23.601: ERROR/AndroidRuntime(5848): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998) 05-05 18:42:23.601: ERROR/AndroidRuntime(5848): at android.widget.LinearLayout.onLayout(LinearLayout.java:918) 05-05 18:42:23.601: ERROR/AndroidRuntime(5848): at android.view.View.layout(View.java:6834) 05-05 18:42:23.601: ERROR/AndroidRuntime(5848): at android.widget.FrameLayout.onLayout(FrameLayout.java:333) 05-05 18:42:23.601: ERROR/AndroidRuntime(5848): at android.view.View.layout(View.java:6834) 05-05 18:42:23.601: ERROR/AndroidRuntime(5848): at android.view.ViewRoot.performTraversals(ViewRoot.java:1016) 05-05 18:42:23.601: ERROR/AndroidRuntime(5848): at android.view.ViewRoot.handleMessage(ViewRoot.java:1666) 05-05 18:42:23.601: ERROR/AndroidRuntime(5848): at android.os.Handler.dispatchMessage(Handler.java:99) 05-05 18:42:23.601: ERROR/AndroidRuntime(5848): at android.os.Looper.loop(Looper.java:123) 05-05 18:42:23.601: ERROR/AndroidRuntime(5848): at android.app.ActivityThread.main(ActivityThread.java:4603) 05-05 18:42:23.601: ERROR/AndroidRuntime(5848): at java.lang.reflect.Method.invokeNative(Native Method) 05-05 18:42:23.601: ERROR/AndroidRuntime(5848): at java.lang.reflect.Method.invoke(Method.java:521) 05-05 18:42:23.601: ERROR/AndroidRuntime(5848): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 05-05 18:42:23.601: ERROR/AndroidRuntime(5848): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 05-05 18:42:23.601: ERROR/AndroidRuntime(5848): at dalvik.system.NativeStart.main(Native Method) 05-05 18:42:25.341: ERROR/PackageInstallationReceiver(146): Remove /data/local/tmp/com.mmb.shop.apk Fail! 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不需設定此項。

 

你可能感兴趣的:(thread,exception,object,ListView,layout,Parameters)