12

I'm trying to set the FrameLayout's width and height based on a Bitmap, what I did was below

        Bitmap theBitmap = BitmapFactory.decodeFile(theFileImage.toString());
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(theBitmap.getWidth(), theBitmap.getHeight());
        frame.setLayoutParams(lp);
        image.setLayoutParams(lp);
        image.setImageBitmap(theBitmap);

but I'm getting a ClassCastException.

What did I do wrong?

Edited:

java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
1
  • stab in the dark but try changing 'LinearLayout.LayoutParams lp' to 'RelativeLayout.LayoutParams'
    – twigg
    Aug 30, 2012 at 8:45

2 Answers 2

15

For setting Layout params, you need to use the inner class LayoutParams of its parent.

For Example : If you are having a LinearLayout inside RelativeLayout and if you need to set the layout params of Linear Layout, you need to use the LayoutParams inner class of RelativeLayout. Else it will gives a ClassCastException.

So in your case, for setting the FrameLayout's Layoutparams , you need to use its parent Layout's Layout Params. Suppose if your layout is like :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<FrameLayout
    android:id="@+id/flContainer"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</FrameLayout>

</RelativeLayout>

Code :

    FrameLayout frame=(FrameLayout) findViewById(R.id.flContainer);  
    ImageView image=(ImageView) findViewById(R.id.image);
    Bitmap theBitmap = BitmapFactory.decodeFile(theFileImage.toString());
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(theBitmap.getWidth(), theBitmap.getHeight());
    frame.setLayoutParams(lp);
    image.setImageBitmap(theBitmap);
4
  • Now I'm getting this error. FATAL EXCEPTION: main 08-30 17:50:24.387: E/AndroidRuntime(5325): at android.view.ViewRoot.performTraversals(ViewRoot.java:941) Aug 30, 2012 at 8:50
  • If your image view is inside the frame layout, try removing the code to set layout params for image view. Check the updated answer and let me know. Aug 30, 2012 at 8:53
  • And the FrameLayout's Layout perfectly fits on the Bitmap. Aug 30, 2012 at 8:59
  • Question, how do i Set the image to that of the LinearLayout? stackoverflow.com/questions/4811905/… (Please help... thanks)
    – Si8
    Jan 29, 2014 at 1:50
1

Seeing the ClassCastException I assume you are doing something illegal here, a couple of questions, what are frame and image?

If frame is a reference to FrameLayout the you would have to use

FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(theBitmap.getWidth(), theBitmap.getHeight());

Let me know if that helps.

4
  • frame is a FrameLayout while image is an ImageView. Aug 30, 2012 at 8:32
  • Ok so is your main container Layout a FrameLayout or a LinearLayout? Aug 30, 2012 at 8:34
  • The parent is a RelativeLayout, FrameLayout is its child, while ImageView is the child of FrameLayout. Aug 30, 2012 at 8:38
  • and I'm also gettings this new error: FATAL EXCEPTION: main 08-30 17:35:54.148: E/AndroidRuntime(32516): at android.view.ViewRoot.performTraversals(ViewRoot.java:941) Aug 30, 2012 at 8:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.