14
        setContentView(R.layout.main2);

        out= new ArrayList<TextView>();
        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 文件。

<RelativeLayout
android:id="@+id/displayPoll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/back_blue_grad"
android:padding="0dp" >


   <TextView
    android:id="@+id/user_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="Priyank Bharad"
    android:textColor="#fff"
    android:textSize="28dp"
    android:textStyle="bold" />
   <ImageView
    android:id="@+id/p_image"
    android:layout_width="35dp"
    android:layout_height="35dp"
    android:layout_marginRight="10dp"
    android:src="@drawable/final_fb" 
    android:layout_toLeftOf="@id/user_name"/>
</RelativeLayout>

</ScrollView>
4

1 回答 1

78

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

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

看一下 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 正在尝试对 to 的实例进行类型转换RelativeLayout.LayoutParams,我认为应该是ScrollView.LayoutParams,因为您RelativeLayoutScrollView.

修复应该是更改您在RelativeLayoutto上使用的布局参数类型ScrollView.LayoutParams

于 2012-04-06T16:25:14.320 回答