Stackoverflow on Android 2.3.3 Devices Only
Asked Answered
W

5

14

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 .
Wether answered 4/11, 2013 at 10:3 Comment(4)
Looks like you have very deeply nested view hierarchy. Try to flatten it and include the relevant view layout (what's in the ScrollView) in the question.Forb
Scrollview : I kept Scrollview under TabHost .Wether
@Forb PLease check the layouts mentioned .Wether
Never seen that much nesting in layout. Please rewrite your Main.xml.Pollak
F
19

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.

Forb answered 8/11, 2013 at 8:7 Comment(1)
@Rizvan, sorry, reorganizing some 1000 lines of XML layout is beyond what is practical on SO. But this answer gives you the reason for the exception and directions to fix it. That is also useful to others with similar problems. Still, you will have to fix your own code. If you have follow-up problems there, you can post a narrower question that can be answered with a paragraph or two, or you can hire a programmer to do the work for you.Forb
B
5

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.

Bristletail answered 4/11, 2013 at 10:40 Comment(3)
How do i know that i should flatten purticularly any layout to relative layout . Please elaborate a bit here .Wether
you will find more info here developer.android.com/training/improving-layouts/…Bristletail
Here is one of the best case studies on android performance I've ever read by Roman Nurik curious-creature.org/2012/12/01/android-performance-case-studyDeppy
E
1

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.

Endoskeleton answered 12/11, 2013 at 18:20 Comment(0)
B
1

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.

Bribe answered 14/11, 2013 at 16:59 Comment(0)
C
0

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

Callan answered 8/11, 2013 at 6:41 Comment(1)
No Its max 20 view , if i going to inflate 2 views also then same thing .Wether

© 2022 - 2024 — McMap. All rights reserved.