Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting Null Pointer Exception in View.getDrawingCache()

I am trying to get View converted in bitmap. In emulator my program is working fine and running as expected but when I run the same code in Device it gives null pointer Exception at particular line :

Bitmap b = Bitmap.createBitmap(view.getDrawingCache());

where view is object of RelativeLayout having Layout of current Activity. Can someone guide how can I resolve this issue. Thanks.

like image 256
Aduait Pokhriyal Avatar asked Jan 25 '26 12:01

Aduait Pokhriyal


1 Answers

You need to call measure and layout on your view before using getDrawingCache(). Otherwise it will return null.

like image 98
ekholm Avatar answered Jan 28 '26 01:01

ekholm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!