Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 383
rozap Avatar asked May 09 '11 17:05

rozap


People also ask

How do I fix null pointer exception could be thrown?

In Java, the java. lang. NullPointerException is thrown when a reference variable is accessed (or de-referenced) and is not pointing to any object. This error can be resolved by using a try-catch block or an if-else condition to check if a reference variable is null before dereferencing it.

What is null pointer exception in junit?

NullPointerException is a runtime exception in Java that occurs when a variable is accessed which is not pointing to any object and refers to nothing or null. Since the NullPointerException is a runtime exception, it doesn't need to be caught and handled explicitly in application code.


3 Answers

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;     } 
like image 87
AZ_ Avatar answered Sep 20 '22 17:09

AZ_


The top level RelativeLayout seems to freak out on measure without boundaries. Set explicit boundaries on the inflated view before calling measure:

v.setLayoutParams(new ViewGroup.LayoutParams(0,0));
like image 20
Eric Ruck Avatar answered Sep 22 '22 17:09

Eric Ruck


Inflater does not extract layout_width and layout_height attributes when you don't assign its parent as an argument in inflate method, so try to set LayoutParams with specified width and height and then call measure.

like image 33
Keyhan Asghari Avatar answered Sep 23 '22 17:09

Keyhan Asghari