Android:nullpointerexception when using Relativelayout with measure ()

Source: Internet
Author: User

When you use the measure () method to calculate the height of a view, you might get an error if the view contains a relativelayout layout:

07-13 13:18:34.330:e/androidruntime (12098): java.lang.NullPointerException
07-13 13:18:34.330:e/androidruntime (12098): at Android.widget.RelativeLayout.onMeasure (relativelayout.java:465)
07-13 13:18:34.330:e/androidruntime (12098): at Android.view.View.measure (view.java:15297)
......

is probably a bug of Android, Android4.4.4 version will not have this problem.

The solution is--
Judge the system version, if it is greater than or equal to 4.4.4 ignore, otherwise:

    1. In this view do not use relativelayout layout, change to linerlayout or framelayout what ...

    2. Before calling the measure () method, determine the value of View.getlayoutparams () and, if NULL, set a layoutparams for the YA.

      View.setlayoutparams (New Viewgroup.layoutparams (desiredwidth,desiredheight));

      Of course, you measure calculate the value may be inaccurate, but at least not crash ah ...
      Oh hehe ...

The first method is recommended.

Sync in: http://www.barryzhang.com/archives/423

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android:nullpointerexception when using Relativelayout with measure ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.