23

When you get an error like the one below (Android 1.5), is it because there are too many nested ViewGroups, or because the total number of Views is too great? Are there any tricks to get around it while still keeping the same look to your app?

java.lang.StackOverflowError
   at android.text.Styled.measureText(Styled.java:371)
   at android.text.Layout.measureText(Layout.java:1600)
   at android.text.Layout.getLineMax(Layout.java:654)
   at android.text.Layout.draw(Layout.java:310)
   at android.text.BoringLayout.draw(BoringLayout.java:356)
   at android.widget.TextView.onDraw(TextView.java:3921)
   at android.view.View.draw(View.java:5838)
   at android.view.ViewGroup.drawChild(ViewGroup.java:1486)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1228)
   at android.view.ViewGroup.drawChild(ViewGroup.java:1484)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1228)
   at android.widget.AbsListView.dispatchDraw(AbsListView.java:1319)
   at android.widget.ListView.dispatchDraw(ListView.java:2820)
   at android.view.View.draw(View.java:5944)
   at android.widget.AbsListView.draw(AbsListView.java:2121)
   at android.view.ViewGroup.drawChild(ViewGroup.java:1486)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1228)
   at android.view.ViewGroup.drawChild(ViewGroup.java:1484)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1228)
   at android.view.View.draw(View.java:5841)
   at android.widget.FrameLayout.draw(FrameLayout.java:352)
   at android.view.ViewGroup.drawChild(ViewGroup.java:1486)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1228)
   at android.view.ViewGroup.drawChild(ViewGroup.java:1484)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1228)
   at android.view.View.draw(View.java:5841)
   at android.widget.FrameLayout.draw(FrameLayout.java:352)
   at android.view.ViewGroup.drawChild(ViewGroup.java:1486)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1228)
   at android.view.View.draw(View.java:5841)
   at android.view.ViewGroup.drawChild(ViewGroup.java:1486)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1228)
   at android.view.View.draw(View.java:5841)
   at android.view.ViewGroup.drawChild(ViewGroup.java:1486)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1228)
   at android.view.View.draw(View.java:5841)
   at android.widget.FrameLayout.draw(FrameLayout.java:352)
   at android.view.ViewGroup.drawChild(ViewGroup.java:1486)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1228)
   at android.view.ViewGroup.drawChild(ViewGroup.java:1484)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1228)
   at android.view.ViewGroup.drawChild(ViewGroup.java:1484)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1228)
   at android.view.View.draw(View.java:5841)
   at android.widget.FrameLayout.draw(FrameLayout.java:352)
   at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1847)
   at android.view.ViewRoot.draw(ViewRoot.java:1217)
   at android.view.ViewRoot.performTraversals(ViewRoot.java:1030)
   at android.view.ViewRoot.handleMessage(ViewRoot.java:1482)
   at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loop(Looper.java:123)
   at android.app.ActivityThread.main(ActivityThread.java:3948)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:521)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
   at dalvik.system.NativeStart.main(Native Method)
Prags
  • 2,457
  • 2
  • 21
  • 38
synic
  • 26,359
  • 20
  • 111
  • 149

7 Answers7

33

You have (way) too many nested layouts. You can check out the official Android blog at d.android.com to get tips and tricks on how to optimize layouts.

Romain Guy
  • 97,993
  • 18
  • 219
  • 200
  • 1
    flatten your view hierarchy by replacing your layouts with relative. Series of articles that Romain suggested here : http://android-developers.blogspot.com/search/label/Optimization – Alex Volovoy May 04 '10 at 16:43
  • 45
    The question is: How much is too much? Is it documented? – neteinstein Mar 30 '12 at 14:28
  • 1
    Update: info about a possible reference value: http://stackoverflow.com/questions/9946368/stackoverflow-caused-by-nested-views – neteinstein Jan 23 '14 at 12:17
  • Indeed it was the problem I have been facing til now. Just had to rearrange some nested views. Thanks! – Marcelo Noguti Dec 03 '14 at 17:24
6

in my case I resolved by in

onCreateView

        rootView = inflater.inflate(R.layout.fragment_interest_list, container);
        return rootView;

by adding attachToRoot parameter

         rootView = inflater.inflate(R.layout.fragment_interest_list,container,false);
         return rootView;
Ishtiaq
  • 1,206
  • 9
  • 18
  • In my case this didn't work, I had to change it to inflater.inflate(R.layout.some_layout, null) – vanomart Dec 10 '15 at 14:39
  • In my case, i previously had : rootView = inflater.inflate(R.layout.fragment_layout, container, true); I set it to : rootView = inflater.inflate(R.layout.fragment_layout, container, false), and voila ! it worked. – Abhinav Puri Mar 12 '16 at 11:21
5

In my case it was caused by nested layouts (complex list itens) in a list inside a fragment view pager inside another fragment, using v4 support and HoloEverywhere v1.4 libraries.

I had to remove nested fragments and build it in a different way.

JPMagalhaes
  • 3,611
  • 1
  • 25
  • 26
2

In my case,

Customized list view has Relative Layout, then it released Stackoverflowerror.

If your listview has relative layout, then erase it :D

akjoshi
  • 15,374
  • 13
  • 103
  • 121
larsien
  • 109
  • 10
  • 1
    In my case this did it for me. Having a ListView, inside a fragment view pager, inside a fragment view pager, with a RelativeLayout was causing crashes on API's < 14. Removing the RelativeLayout re-working the listview's layout did the trick (and performance is also much better). – dell116 Jan 13 '15 at 18:59
0

In my case that was ListView inside a ScrollView and when I try to Scroll it to the bottom. It produces stackoverflow error.

So, avoid to have ListView inside ScrollView.

Or we can use different approach to implement ListView.

Android list view inside a scroll view

Community
  • 1
  • 1
Vinit Siriah
  • 327
  • 3
  • 12
0

In my case and in XamarinForms, I solved this error by adding CachingStrategy="RecycleElement" to my ListView and also removing extra layouts.
Android users should use RecyclerView or other view recycling methods.

Dr TJ
  • 3,241
  • 2
  • 35
  • 51
0

For Xamarin Forms in our case using ListViews didn't resolve the issue, the exception kept raising on a carousel we have, and we can't call the draw method from another thread because some graphics painted with NControl we need doesn't allow us to do that (OpenGL related I guess). So in the end just catching the exception in our carousel custom renderer and calling Invalidate() on it resolved the problem.