Android java.lang.StackOverFlow 错误。嫌浏览量太多?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2762924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.StackOverFlow error. Suspected too many views?
提问by synic
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?
当出现如下错误(Android 1.5)时,是因为嵌套的ViewGroups太多,还是因为View的总数太大?有什么技巧可以解决这个问题,同时仍然保持你的应用程序的外观?
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)
回答by Romain Guy
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.
您(方式)有太多嵌套布局。您可以在 d.android.com 上查看官方 Android 博客,以获取有关如何优化布局的提示和技巧。
回答by Ishtiaq
in my case I resolved by in
在我的情况下,我解决了
onCreateView
rootView = inflater.inflate(R.layout.fragment_interest_list, container); return rootView;
by adding attachToRootparameter
rootView = inflater.inflate(R.layout.fragment_interest_list,container,false); return rootView;
创建视图
rootView = inflater.inflate(R.layout.fragment_interest_list, container); return rootView;
通过添加attachToRoot参数
rootView = inflater.inflate(R.layout.fragment_interest_list,container,false); return rootView;
回答by JPMagalhaes
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.
在我的情况下,它是由使用 v4 支持和 HoloEverywhere v1.4 库的另一个片段内的片段视图寻呼机内的列表中的嵌套布局(复杂列表 itens)引起的。
I had to remove nested fragments and build it in a different way.
我不得不删除嵌套的片段并以不同的方式构建它。
回答by larsien
In my case,
就我而言,
Customized list view has Relative Layout, then it released Stackoverflowerror
.
自定义列表视图有相对布局,然后发布Stackoverflowerror
。
If your listview
has relative layout, then erase it :D
如果您listview
有相对布局,则将其擦除:D
回答by Vinit Siriah
In my case that was ListView
inside a ScrollView
and when I try to Scroll it to the bottom. It produces stackoverflow error.
在我的情况下,它ListView
在 a 内ScrollView
,当我尝试将其滚动到底部时。它会产生计算器溢出错误。
So, avoid to have ListView
inside ScrollView
.
所以,避免在ListView
里面ScrollView
。
Or we can use different approach to implement ListView.
或者我们可以使用不同的方法来实现 ListView。
回答by Dr TJ
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.
在我的情况和 XamarinForms 中,我通过添加CachingStrategy="RecycleElement"
到我的ListView
并删除额外的布局解决了这个错误。
Android 用户应使用RecyclerView
或其他视图回收方法。
回答by Pablo López Martínez
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.
对于我们使用 ListViews 的案例中的 Xamarin Forms 没有解决问题,异常在我们拥有的轮播上不断引发,我们无法从另一个线程调用 draw 方法,因为我们需要的一些用 NControl 绘制的图形不允许我们这样做(我猜是与OpenGL相关的)。因此,最终在我们的轮播自定义渲染器中捕获异常并对其调用 Invalidate() 即可解决问题。