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!
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.
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.
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; }
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));
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With