Android carefully use layout nesting, as far as possible to control under the 5 layer Java.lang.StackOverflowError

Source: Internet
Author: User

first, to explore the reasonsAn Android interface with more layout embedded in a complex layout. On Android 2.3, low-memory models, appear java.lang.StackOverflowError this exception,appcrash exit.

This error appears strange, when I do technical research, the interface is placed in the 4.0 system test, the work is very normal, there is no such a serious error.

The scenario description for this error in the Android SDK is described as: because there are too many levels of stack calls. The maximum limit of the virtual Dalvik (as a virtual-device parameter can be customized) is exceeded.       When a programming error results in an infinite recursive invocation, the program is correct but the hierarchy of recursive calls is too deep to trigger. In the process of looking for a problem, take the elimination method. A little bit of stare code. A problem was found because of a voice control. The voice control is removed and crash does not appear. Observing the integration of the interface, it is found that when the voice control is added, a layer of layout has been introduced, which shows that the nesting of layout has reached the maximum value and cannot tolerate the addition of 1 layers. Because of the complexity of the interface, the layout depth of the view is 6 layers at design time.

The reason is that the interface nesting level is too deep, the initial identification solution to reduce the layout level. Second, the solution to solve the deep nesting of the basic idea is to achieve the same effect on the premise of reducing the layout depth of the interface.

This error was resolved when I reduced the layout depth from 6 layers to 4 layers.    The solution provided by the SDK is: 1) Use the various layout properties of Relativelayout to complete the layout, and avoid using nested layout too much. 2) occupy or fill the remaining space of the elements of multi-use view, avoid the use of layout and other viewgroup. The final method has : Because my view level does need a lot of, so from changing the view level to start very difficult, began to tangle for very long, finally found a breakthrough. The Official Android SDK provides an XML tag that is labeled in the official documentation to reduce the view hierarchy by using the merge tag.

This is the problem for the moment, and in development, be careful not to add depth to the existing depth .

At the same time continue to look for further solutions.


In summary, from the actual data, the layout depth of the design is more than 10 layers (suppose to be compatible with low-end models, preferably not more than 5 layers). You should consider the optimization, otherwise there will be java.lang.StackOverflowError crash.

Android carefully use layout nesting, as far as possible to control under the 5 layer Java.lang.StackOverflowError

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.