Stackoverflow on Android 2.3.3 Devices Only

二次信任 提交于 2019-12-03 00:37:07
laalto

Your view hierarchy is too deep. When the hierarchy is drawn recursively, you run out of stack space and cannot go any deeper in the hierarchy, as observed in the stack trace.

Why this only occurs in earlier devices is because UI thread stack size is only 12kB in Android 2.3 devices but 16kB in later OS versions (ref).

How to reduce view hierarchy depth? Simply avoid nesting layouts whenever possible. The layouts you posted are too complex, no point fixing them for you within this Stack Overflow model (it takes some time to do correctly and it is likely too specific to help others), but here are some general guidelines:

  • Remove unnecessary layouts. For example, in your firsttab_results.xml you only need the TableLayout, the RelativeLayout and LinearLayout on top are pretty much useless. In your ScrollView there are at least 6 nested layouts when one or at most two would likely be enough.

  • A single child in a layout is a code smell. Most of the time the same can be achieved moving the child to its parent layout and adjusting the layout with margins.

  • There's usually little need to put layouts inside a RelativeLayout. Relative layout is powerful at laying out its children using relative positioning and child baseline alignment.

  • If you need a nested layout just for a background, try moving the background to e.g. just a View that is of the same size as the nested layout would be and laying out the other elements on top of it.

  • Pay attention to Android Lint warnings. The tool has become quite good at detecting view hierarchy complexities that could be simplified.

You have nested view hierarchy you can see your view hierarchy using hierarchyViewer in android. if you have many nested view in your app then some devices those have small memory those give exception and for other it run well.flatten your views. use RelativeLayout to flatten views.

I dont think your seeing your error due to "nesting" your views. I think it may be an issue using something that earlier devices dont have access to in their library and your ide isnt catching it or you have lint warnings and are ignoring them. Try looking at your lint warnings and see if it mentions anything about certain views or api's not being available in earlier versions.

Dog

The stack on Android is limited to 8K, even on at least the Samsung Galaxy S3, which has 2GB RAM. I don't know whether it differs depending on device. I don't know why, I asked and they closed my question.

Your Main.xml is very nested, whatever code uses it is probably causing the stack overflow. The amount of stack used per level of nesting is implementation dependent, this is why it happens on one version and not the other. One version may call a chain of 5 functions which then handles the next level of the nested XML, and so on, and one may chain 20.

Are you using recursion to build your view hierarchy? I almost never see a StackOverflowError without recursion.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!