sam
sam

Reputation: 95

Android java.lang.ClassCastException

I am loading a webview with a close button and a slide button. When I clic

main.xml

<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#FFFFFF" 
    android:layout_marginRight="25dip"
    android:layout_marginTop="25dip"
    android:layout_marginLeft="25dip"
    android:layout_marginBottom="25dip" >
<WebView
    android:id="@+id/webac_larger"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
</WebView>

<ImageView android:id="@+id/close_btn"         
    android:layout_alignParentTop="true"         
    android:layout_alignParentRight="true"         
    android:layout_width="wrap_content"         
    android:layout_height="wrap_content"
    android:src="@drawable/close_button"
    android:layout_marginTop="-3dip"
    android:layout_marginRight="0dip"
    />  

<ImageButton 
    android:id="@+id/slidebutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:layout_toLeftOf="@+id/close_btn"
    android:src="@drawable/arrowleft" />     

I want to resize webview on click of a button.

final ImageButton button1 = (ImageButton) findViewById(R.id.slidebutton);
        button1.setOnClickListener(new OnClickListener() {
          public void onClick(View v) {
            Log.d("MultiWeb", "I have clicked the button");
            RelativeLayout params = ((RelativeLayout )button1.getParent());
             params.setLayoutParams(new RelativeLayout.LayoutParams(100, 100));
          }
        });

I am getting this error. I am using relatice layout and why error says something about frame layout ?

java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams

Upvotes: 2

Views: 1323

Answers (1)

quietmint
quietmint

Reputation: 14153

As stated in the Android Layouts API Guide, each type of ViewGroup has its own LayoutParams class that define the size and position for child Views.

Instead of creating a brand new instance of one of the LayoutParams classes and risking a ClassCastException, you can call getLayoutParams() on your view to get the current parameter object, modify it as you wish, and call setLayoutParams() to apply the updated parameters.

Upvotes: 1

Related Questions