Question

I been working on Native Android App where i face some bug on purticularly Android 2.3.3 versions and below Android 3.0 version .

I am not getting where exactly am falling in my code because in Logcat Every Line of code compiled but at the end am getting very strange error describe log below:

java.lang.StackOverflowError
at java.util.concurrent.locks.ReentrantLock$NonfairSync.tryAcquire(ReentrantLock.java:189)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(AbstractQueuedSynchronizer.java:1171)
at java.util.concurrent.locks.ReentrantLock$NonfairSync.lock(ReentrantLock.java:185)
at java.util.concurrent.locks.ReentrantLock.lock(ReentrantLock.java:261)
at java.util.concurrent.CopyOnWriteArrayList.removeRange(CopyOnWriteArrayList.java:569)
at java.util.concurrent.CopyOnWriteArrayList.remove(CopyOnWriteArrayList.java:366)
at java.util.concurrent.CopyOnWriteArrayList.remove(CopyOnWriteArrayList.java:376)
at android.view.ViewTreeObserver.removeOnPreDrawListener(ViewTreeObserver.java:377)

at android.widget.TextView.onDraw(TextView.java:4085)
at android.view.View.draw(View.java:6986)
at android.view.ViewGroup.drawChild(ViewGroup.java:1739)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1466)
at android.view.ViewGroup.drawChild(ViewGroup.java:1737)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1466)
at android.view.ViewGroup.drawChild(ViewGroup.java:1737)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1466)
at android.view.View.draw(View.java:6989)
at android.view.ViewGroup.drawChild(ViewGroup.java:1739)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1466)
at android.view.View.draw(View.java:6989)
at android.view.ViewGroup.drawChild(ViewGroup.java:1739)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1466)
at android.view.ViewGroup.drawChild(ViewGroup.java:1737)

    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1466)
at android.view.ViewGroup.drawChild(ViewGroup.java:1737)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1466)
at android.view.View.draw(View.java:6989)
at android.view.ViewGroup.drawChild(ViewGroup.java:1739)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1466)
at android.view.View.draw(View.java:6989)
at android.view.ViewGroup.drawChild(ViewGroup.java:1739)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1466)
at android.view.ViewGroup.drawChild(ViewGroup.java:1737)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1466)
at android.view.ViewGroup.drawChild(ViewGroup.java:1737)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1466)
at android.view.View.draw(View.java:6989)
at android.widget.FrameLayout.draw(FrameLayout.java:361)
at android.view.ViewGroup.drawChild(ViewGroup.java:1739)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1466)
at android.view.ViewGroup.drawChild(ViewGroup.java:1737)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1466)

    at android.view.View.draw(View.java:6989)
at android.view.ViewGroup.drawChild(ViewGroup.java:1739)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1466)
at android.view.View.draw(View.java:6989)
at android.view.ViewGroup.drawChild(ViewGroup.java:1739)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1466)
at android.view.View.draw(View.java:7093)
at android.widget.FrameLayout.draw(FrameLayout.java:361)
at android.widget.ScrollView.draw(ScrollView.java:1421)
at android.view.ViewGroup.drawChild(ViewGroup.java:1739)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1466)
at android.view.ViewGroup.drawChild(ViewGroup.java:1737)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1466)
at android.view.View.draw(View.java:6989)
at android.widget.FrameLayout.draw(FrameLayout.java:361)
at android.view.ViewGroup.drawChild(ViewGroup.java:1739)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1466)
at android.view.ViewGroup.d

As i checked my code i found no trace for stackoverflow . When i run same code on android 3.0 or above version it runs effectively and correct but not on android 2.3.3 & below 3.0 version . Please let me know if any one know about this issue ??

[EDIT] I am using Tab activity so main layout is Main.xml and in that sub activity holds view called firstTab_Results.xml , it holds a table layout which indeed performs the inflating max 20 views of inflate_table_firstTab.xml layout .

[EDIT 2] I am not recursing it anyway , it inflate Max 20 views(inflate_table_firstTab.xml) to table layout (firstTab_Results.xml) .

Note : Stackoverflow happens on single or dual core processor devices & without fullscreen mode .
Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top