Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
251 views
in Technique[技术] by (71.8m points)

android - Getting ClassCastException when trying to insert RelativeLayout dyanmically?

        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+"





");
        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+"





");

                llay.addView(out.get(i));

            }

                Log.v("my","rasasched");

By executing the program I am getting this error

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)

This is the XML file i m using..

<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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The mistake here is in the choice of the subclass of LayoutParams.

The correct version of LayoutParams to instantiate depends on the parent of the view you're creating, not the type of the view itself.

Take a look at the logcat dump:

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 is trying to typecast the instance of RelativeLayout.LayoutParams to, what I believe should be ScrollView.LayoutParams, because your RelativeLayout is inside of a ScrollView.

The fix should be to change the layout params type that you're using on the RelativeLayout to ScrollView.LayoutParams.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...