Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>
like image 932
Priyank Bharad Avatar asked Apr 06 '12 15:04

Priyank Bharad


People also ask

What is layout_ alignparentbottom?

android:layout_alignParentBottomIf true, makes the bottom edge of this view match the bottom edge of the parent. Accommodates bottom margin. May be a boolean value, such as " true " or " false ".

What is a ViewGroup?

ViewGroup is a collection of Views(TextView, EditText, ListView, etc..), somewhat like a container. A View object is a component of the user interface (UI) like a button or a text box, and it's also called a widget.

What is child view in android?

In android (and most other technologies), views can have subviews, aka "children". Most views can have children and can be a child of a parent view. It's kind of like a "tree". The most obvious feature of being a child of some other view is that the child moves with the parent view.

Which of the following is a view Group?

Android supports the following ViewGroups: LinearLayout. AbsoluteLayout. TableLayout.


1 Answers

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.

like image 170
wsanville Avatar answered Nov 12 '22 20:11

wsanville