Solution to null pointer problem when Android converts view to bitmap

Solution to null pointer problem when Android converts view to bitmap

When working on Android projects, sometimes there may be a need to convert a view or a layout file into a bitmap object.

In fact, most of the methods are the same. But there are some small details to pay attention to. This function has been used in the project recently. Now share it. I hope it can help you meet this result

People with problems.

The first is the converted code:

The above is the method of converting to bitmap, but note that view.measurespec.makemeasurespec (0, view. Measurespec. Unspecified) is used

When measuring the view, (if your layout contains relativelayout) the API is 17 or lower, it will contain null pointer exception. Encountered this problem in the project

I don't know what's going on. I found it later when I looked at the source code. The following is the official explanation of this method:

This bug is only fixed in systems above API 17. Here are two solutions:

1. Do not include relativelayout layout in the layout file

2. It seems that view. Measurespec. Makemeasurespec (256, view. Measurespec. Actual) can also be used

The above is the solution to the null pointer problem when Android converts view to bitmap. If you have any questions, please leave a message or communicate and discuss with our community. Thank you for your support!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>