Question

I got this error: java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams

I think the error is caused by the fact that i have a relative layout in a linear layout I looked for same problems:Set RelativeLayout layout params programmatically - throws ClassCastException

But this didn't help

My code :

LinearLayout.LayoutParams paramsT1score = (LinearLayout.LayoutParams)mT1layoutScore.getLayoutParams();
                // Changes the height and width to the specified *pixels*
                paramsT1score.height = 80;
                mT1layout.setLayoutParams(paramsT1score);

This is my .xml

</RelativeLayout>



<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="fill_parent"
    android:layout_height="70dp"
    android:orientation="horizontal" >

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="70dp"
        android:background="#314ebd"
        android:id="@+id/LayoutT1score"
        android:layout_toLeftOf="@+id/LayoutT2score">

        <TextView
            android:id="@+id/player1"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="number"
            android:text="0"
            android:textSize="24dp"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="10dp"
            android:textColor="#ffffff" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Team 1"
            android:id="@+id/lblTeam1"
            android:layout_above="@+id/player1"
            android:layout_alignLeft="@+id/player1"
            android:layout_alignStart="@+id/player1"
            android:textSize="24dp"
            android:textColor="#ffffff" />

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="60dp"
        android:id="@+id/LayoutT2score"
        android:background="#fbff62"
        >

        <TextView
            android:id="@+id/player2"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="number"
            android:text="0"
            android:textSize="24dp"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="0dp"
            android:textColor="#000000" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Team 2"
            android:id="@+id/lblTeam2"
            android:textSize="24dp"
            android:textColor="#000000"
            android:layout_above="@+id/player2"
            android:layout_alignLeft="@+id/player2"
            android:layout_alignStart="@+id/player2" />


    </RelativeLayout>
</LinearLayout>

Was it helpful?

Solution

Layout params type is determined by the parent type. You get the layout params from one view mT1layoutScore and then try to set up them in another mT2layout the parent of which is of a different type.

Set the layout params on the same object, or since you're editing the layout params in-place, just call mT1layoutScore.requestLayout().

OTHER TIPS

Hm, seems like params of mT1layoutScore is LinearLayout.LayoutParams, but params of mT2layout is RelativeLayout.LayoutParams.

So, i think you should create

RelativeLayout.LayoutParams paramsT2score = (RelativeLayout.LayoutParams)mT2layout.getLayoutParams();

then copy all needed params from paramsT1score to paramsT2score, and then added

mT2layout.setLayoutParams(paramsT2score);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top