rozap rozap - 6 months ago 55
Android Question

Null pointer exception when calling measure on inflated layout

So I have a layout that is a separate file, so I inflate it. Then I change my text views and image in my layout, and call measure, but I get a null pointer when I try to measure the layout. I cannot for the life of me figure out why this is. I'm trying to convert the layout to a bitmap eventually.

View inflatedView = ((Activity)getContext()).getLayoutInflater().inflate(R.layout.my_layout, null);

inflatedView.measure(getWidth(), getHeight());
inflatedView.layout(0, 0, inflatedView.getMeasuredWidth(),inflatedView.getMeasuredHeight());
Bitmap viewAsImage = Bitmap.createBitmap(inflatedView.getWidth(), inflatedView.getHeight(), Bitmap.Config.ARGB_8888);
Canvas viewCanvas = new Canvas(viewAsImage);


Here is the XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_gravity="bottom"
android:id="@+id/my_layout"
android:background="@color/transparent_black">
<ImageView android:id="@+id/image_left"
android:layout_width="48dip" android:layout_height="48dip"
android:layout_centerVertical="true" android:layout_alignParentLeft="true"
android:scaleType="fitXY" android:maxWidth="48dip" android:maxHeight="48dip"
android:padding="5dip" />
<ImageView android:id="@+id/image_right"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_marginRight="20dip" android:src="@drawable/arrow"
android:layout_centerVertical="true" android:scaleType="fitXY"
android:maxWidth="48dip" android:maxHeight="48dip"
android:layout_alignParentRight="true" />
<TextView android:paddingLeft="4dip" android:paddingRight="1dip"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_toRightOf="@id/image_left"
android:layout_toLeftOf="@id/image_right" android:id="@+id/some_name"
android:maxLines="1" android:ellipsize="end" android:textSize="20sp"
android:textColor="@color/white"
android:layout_centerVertical="true"
android:textStyle="normal" />
</RelativeLayout>


At the measure() line, it throws a null pointer, and I have verified that getWidth and getHeight are giving legitimate values.

Any ideas?

Thanks!

AZ_ AZ_
Answer

measure() throws Null Pointer Exception because when you inflate you layout it won't read layout height width properties.

Just add this line before calling view.measure() depending upon type of layout you are using

v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

public Bitmap getBitmapFromView(RelativeLayout v) {
        v.setLayoutParams(new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
        v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
        Bitmap b = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);

        Canvas c = new Canvas(b);
        v.draw(c);
        return b;
    }
Comments