ConstraintLayout因StackOverflowError

时间:2018-10-29 13:37:12

标签: android android-constraintlayout

上周我通过不同设备的Firebase Crashlytics收到了多个奇怪的崩溃报告。 他们是:

  • Android 4(4.2-4.4)的100%
  • 大部分(98%)来自三星平板电脑

堆栈跟踪是相同的,并且不包含对我的代码的任何引用,因此我不知道崩溃发生在哪里以及为什么。搜索我的异常没有任何帮助。因此,我希望有人可以帮助我调查崩溃的情况。据我了解,崩溃发生在ConstraintLayout库(我使用版本1.1.3)中。

崩溃堆栈跟踪:

Fatal Exception: java.lang.StackOverflowError
   at java.util.HashMap$KeySet.<init>(HashMap.java:911)
   at java.util.HashMap$KeySet.<init>(HashMap.java:911)
   at java.util.HashMap.keySet(HashMap.java:696)
   at java.util.HashSet.iterator(HashSet.java:161)
   at android.support.constraint.solver.widgets.ResolutionNode.didResolve(ResolutionNode.java:70)
   at android.support.constraint.solver.widgets.ResolutionAnchor.resolve(ResolutionAnchor.java:140)
   at android.support.constraint.solver.widgets.ResolutionNode.didResolve(ResolutionNode.java:71)
   at android.support.constraint.solver.widgets.ResolutionAnchor.resolve(ResolutionAnchor.java:140)
----A lot of identical lines---- 
   at android.support.constraint.solver.widgets.ResolutionNode.didResolve(ResolutionNode.java:71)
   at android.support.constraint.solver.widgets.ResolutionAnchor.resolve(ResolutionAnchor.java:85)
   at android.support.constraint.solver.widgets.ConstraintWidgetContainer.solveGraph(ConstraintWidgetContainer.java:586)
   at android.support.constraint.solver.widgets.ConstraintWidgetContainer.optimize(ConstraintWidgetContainer.java:643)
   at android.support.constraint.solver.widgets.ConstraintWidgetContainer.layout(ConstraintWidgetContainer.java:328)
   at android.support.constraint.ConstraintLayout.solveLinearSystem(ConstraintLayout.java:1860)
   at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:1621)
   at android.view.View.measure(View.java:16749)
   at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1235)
   at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
   at android.widget.ScrollView.onMeasure(ScrollView.java:327)
   at android.view.View.measure(View.java:16749)
   at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:1227)
   at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:1572)
   at android.view.View.measure(View.java:16749)
   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5295)
   at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
   at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
   at android.view.View.measure(View.java:16749)
   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5295)
   at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1413)
   at android.widget.LinearLayout.measureVertical(LinearLayout.java:696)
   at android.widget.LinearLayout.onMeasure(LinearLayout.java:589)
   at android.view.View.measure(View.java:16749)
   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5295)
   at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
   at android.view.View.measure(View.java:16749)
   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5295)
   at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1413)
   at android.widget.LinearLayout.measureVertical(LinearLayout.java:696)
   at android.widget.LinearLayout.onMeasure(LinearLayout.java:589)
   at android.view.View.measure(View.java:16749)
   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5295)
   at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
   at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2439)
   at android.view.View.measure(View.java:16749)
   at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2072)
   at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1175)
   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1372)
   at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1062)
   at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5996)
   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
   at android.view.Choreographer.doCallbacks(Choreographer.java:574)
   at android.view.Choreographer.doFrame(Choreographer.java:544)
   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
   at android.os.Handler.handleCallback(Handler.java:733)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:136)
   at android.app.ActivityThread.main(ActivityThread.java:5590)
   at java.lang.reflect.Method.invokeNative(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:515)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
   at dalvik.system.NativeStart.main(NativeStart.java)

请帮助我找出崩溃的原因或至少如何重现它。

UPD:

在用户的帮助下,我找到了一个发生崩溃的地方。这是一项活动,其中的布局不是从xml扩展,而是通过代码(以ConstraintLayout为根)在onCreate()中生成。

1 个答案:

答案 0 :(得分:0)

最后,我找出了原因。

我有一个活动,其中根据传递给活动的数据和每个项目的类型的列表动态添加了一些视图。它们已添加到ScrollView的ConstraintLayout中。添加的观看次数超过约30时会发生崩溃。

解决方案: 我改用RecyclerView进行更有效的绘图并优化内存使用率,问题却不复存在。不要重复我的错误,并使用RecyclerView显示大列表。主要错误是我无法想象情况,那里的名单确实很大,但我们在这里。

这不能解释为什么仅在Samsung设备上以及仅在Android 4.2-4.4上发生崩溃,但是无论如何,使用RecyclerView是更好的解决方案。