Android, do you know how many layers will Crash after the interface layout is nested? androidcrash

Source: Internet
Author: User

Android, do you know how many layers will Crash after the interface layout is nested? androidcrash

Let's first put a diagram of Hierarchy Viewer: (simulator Android4.4)

 

Do you see the number 6? The RelativeLayout is the root ViewGroup of MainActivity, and there are 5 layers before RelativeLayout, that is, the default system interface DecorView (FrameLayout) --> LinearLayout --> FramelLayout --> ActionBarOverlayLayout --> ContentFrameLayout.

Let's talk about the result directly. The simulator can be used to nest a maximum of 39 layers on API19, that is, Android4.4, including the default layer 5 of the system. That is to say, we can write a maximum of 34 layers of nesting.

When the number of nested layers reaches 40, the following exception is reported,

10-27 11:38:53. 901 25582-25582/com. aquarius. customview E/AndroidRuntime: fatal exception: main Process: com. aquarius. customview, PID: 25582 java. lang. stackOverflowError at android. util. pools $ SynchronizedPool. acquire (Pools. java: 154) at android. view. GLES20RecordingCanvas. obtain (GLES20RecordingCanvas. java: 43) at android. view. GLES20DisplayList. start (GLES20DisplayList. java: 61) at android. view. view. getDisplayList (View. java: 13323) at android. view. view. getDisplayList (View. java: 13404) at android. view. view. draw (View. java: 14182) at android. view. viewGroup. drawChild (ViewGroup. java: 3103) at android. view. viewGroup. dispatchDraw (ViewGroup. java: 2940) at android. view. view. getDisplayList (View. java: 13357) at android. view. view. getDisplayList (View. java: 13404) at android. view. view. draw (View. java: 14182) at android. view. viewGroup. drawChild (ViewGroup. java: 3103) at android. view. viewGroup. dispatchDraw (ViewGroup. java: 2940) at android. view. view. getDisplayList (View. java: 13357) at android. view. view. getDisplayList (View. java: 13404) at android. view. view. draw (View. java: 14182) at android. view. viewGroup. drawChild (ViewGroup. java: 3103) at android. view. viewGroup. dispatchDraw (ViewGroup. java: 2940) at android. view. view. getDisplayList (View. java: 13357) at android. view. view. getDisplayList (View. java: 13404) at android. view. view. draw (View. java: 14182) at android. view. viewGroup. drawChild (ViewGroup. java: 3103) at android. view. viewGroup. dispatchDraw (ViewGroup. java: 2940) at android. view. view. getDisplayList (View. java: 13357) at android. view. view. getDisplayList (View. java: 13404) at android. view. view. draw (View. java: 14182) at android. view. viewGroup. drawChild (ViewGroup. java: 3103) at android. view. viewGroup. dispatchDraw (ViewGroup. java: 2940) at android. view. view. getDisplayList (View. java: 13357) at android. view. view. getDisplayList (View. java: 13404) at android. view. view. draw (View. java: 14182) at android. view. viewGroup. drawChild (ViewGroup. java: 3103) at android. view. viewGroup. dispatchDraw (ViewGroup. java: 2940) at android. view. view. getDisplayList (View. java: 13357) at android. view. view. getDisplayList (View. java: 13404) at android. view. view. draw (View. java: 14182) at android. view. viewGroup. drawChild (ViewGroup. java: 3103) at android. view. viewGroup. dispatchDraw (ViewGroup. java: 2940) at android. view. view. getDisplayList (View. java: 13357) at android. view. view. getDisplayList (View. java: 13404) at android. view. view. draw (View. java: 14182) at android. view. viewGroup. drawChild (ViewGroup. java: 3103) at android. view. viewGroup. dispatchDraw (ViewGroup. java: 2940) at android. view. view. getDisplayList (View. java: 13357) at android. view. view. getDisplayList (View. java: 13404) at android. view. view. draw (View. java: 14182) at android. view. viewGroup. drawChild (ViewGroup. java: 3103) at android. view. viewGroup. dispatchDraw (ViewGroup. java: 2940) at android. view. view. getDisplayList (View. java: 13357) at android. view. view. getDisplayList (View. java: 13404) at android. view. view. draw (View. java: 14182) at android. view. viewGroup. drawChild (ViewGroup. java: 3103) at android. view. viewGroup. dispatchDraw (ViewGroup. java: 2940) at android. view. view. getDisplayList (View. java: 13357) at android. view. view. getDisplayList (View. java: 13404) at android. view. view. draw (View. java: 14182) at android. view. viewGroup. drawChild (ViewGroup. java: 3103) at android. view. viewGroup. dispatchDraw (ViewGroup. java: 2940) at android. view. view. getDisplayList (View. java: 13357) at android. view. view. getDisplayList (View. java: 13404) at android. view. view. draw (View. java: 14182) at android. view. viewGroup. drawChild (ViewGroup. java: 3103) at android. view. viewGroup. dispatchDraw (ViewGroup. java: 2940) at android. view. view. getDisplayList (View. java: 13357) at android. view. view. getDisplayList (View. java: 13404) at android. view. view. draw (View. java: 14182) at android. view. viewGroup. drawChild (ViewGroup. java: 3103) at android. view. viewGroup. dispatchDraw (ViewGroup. java: 2940) at android. view. view. getDisplayList (View. java: 13357) at android. view. view. getDisplayList (View. java: 13404) at android. view. view. draw (View. java: 14182) at android. view. viewGroup. drawChild (ViewGroup. java: 3103) at android. view. viewView Code


More layers can be nested on API23 Android6.0. crash is still unavailable after more than 40 layers of experiments.

I have not tested the specific results of versions earlier than 4.4, and Android open-source mobile phone manufacturers may modify the logic. However, considering the backward compatibility issue, we should not nest too many layers,

When you write more than 10 layers of layout layers, you should consider optimizing the layout, because even if the application does not crash due to too many nesting layers, rendering time is too long, the UI smoothness is affected.

 

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.