尝试以dyanmically插入RelativeLayout时获取ClassCastException?

setContentView(R.layout.main2); out= new ArrayList(); llay =(RelativeLayout) findViewById(R.id.displayPoll); RelativeLayout.LayoutParams lparams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); RelativeLayout.LayoutParams lparams1 = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); LayoutParams lpar = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); Log.v("my","rched"); out.add(new TextView(this)); out.get(0).setLayoutParams(lpar); out.get(0).setId(1); lparams1.addRule(RelativeLayout.ALIGN_BOTTOM,R.id.user_name); llay.setLayoutParams(lparams1); out.get(0).setText("Title "+0+"\n\n\n\n\n\n"); my_poll.this.llay.addView(out.get(0)); for (int i = 1; i < 3;i++)//mJsonArray.length(); i++) { out.add(new TextView(this)); out.get(i).setLayoutParams(lpar); out.get(i).setId(i+1); Log.v("my","hey : "+out.get(i-1).getId()); lparams.addRule(RelativeLayout.BELOW,out.get(i-1).getId()); llay.setLayoutParams(lparams); out.get(i).setText("Title "+i+"\n\n\n\n\n\n"); llay.addView(out.get(i)); } Log.v("my","rasasched"); 

通过执行程序,我收到此错误

 04-06 20:30:49.372: E/AndroidRuntime(1093): FATAL EXCEPTION: main 04-06 20:30:49.372: E/AndroidRuntime(1093): java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams 04-06 20:30:49.372: E/AndroidRuntime(1093): at android.widget.ScrollView.onMeasure(ScrollView.java:301) 04-06 20:30:49.372: E/AndroidRuntime(1093): at android.view.View.measure(View.java:8171) 04-06 20:30:49.372: E/AndroidRuntime(1093): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132) 04-06 20:30:49.372: E/AndroidRuntime(1093): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245) 04-06 20:30:49.372: E/AndroidRuntime(1093): at android.view.View.measure(View.java:8171) 04-06 20:30:49.372: E/AndroidRuntime(1093): at android.widget.LinearLayout.measureVertical(LinearLayout.java:526) 04-06 20:30:49.372: E/AndroidRuntime(1093): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304) 04-06 20:30:49.372: E/AndroidRuntime(1093): at android.view.View.measure(View.java:8171) 04-06 20:30:49.372: E/AndroidRuntime(1093): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132) 04-06 20:30:49.372: E/AndroidRuntime(1093): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245) 04-06 20:30:49.372: E/AndroidRuntime(1093): at android.view.View.measure(View.java:8171) 04-06 20:30:49.372: E/AndroidRuntime(1093): at android.view.ViewRoot.performTraversals(ViewRoot.java:801) 04-06 20:30:49.372: E/AndroidRuntime(1093): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727) 04-06 20:30:49.372: E/AndroidRuntime(1093): at android.os.Handler.dispatchMessage(Handler.java:99) 04-06 20:30:49.372: E/AndroidRuntime(1093): at android.os.Looper.loop(Looper.java:123) 04-06 20:30:49.372: E/AndroidRuntime(1093): at android.app.ActivityThread.main(ActivityThread.java:4627) 04-06 20:30:49.372: E/AndroidRuntime(1093): at java.lang.reflect.Method.invokeNative(Native Method) 04-06 20:30:49.372: E/AndroidRuntime(1093): at java.lang.reflect.Method.invoke(Method.java:521) 04-06 20:30:49.372: E/AndroidRuntime(1093): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 04-06 20:30:49.372: E/AndroidRuntime(1093): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 04-06 20:30:49.372: E/AndroidRuntime(1093): at dalvik.system.NativeStart.main(Native Method) 

这是我正在使用的XML文件..

      

这里的错误在于选择LayoutParams的子类。

要实例化的LayoutParams的正确版本取决于您正在创建的视图的父级而不是视图本身的types

看看logcat转储:

 04-06 20:30:49.372: E/AndroidRuntime(1093): java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams 04-06 20:30:49.372: E/AndroidRuntime(1093): at android.widget.ScrollView.onMeasure(ScrollView.java:301) 

Android正试图将RelativeLayout.LayoutParams的实例强制转换为我认为应该是ScrollView.LayoutParams ,因为您的RelativeLayout位于ScrollView内部。

修复应该是将您在RelativeLayout上使用的布局参数types更改为ScrollView.LayoutParams