Android开发中StackOverflowError错误实例分析
一、概述
我在一个复杂的layout嵌套较多的android界面,碰到了java.lang.StackOverflowError这个Fatal Exception,app程序crash退出。这个错误出现的比较奇怪,在我做技术调研的时候,这个界面是放在单独的一个程序中展示的,工作很正常,没有出现这个严重错误,当将其嵌入到一个ActivityGroup后才报错。

android SDK中对该错误的出现的场景描述为:由于栈调用的层次太多,超过了虚拟器dalvik的最大限制(作为一个虚拟器参数,可定制)导致的。当程序编写错误导致无限递归调用时会触发,此外程序正确但是递归调用的层次过深也会触发。

结合我的界面的情况,在单独程序中运行时没有问题,则说明不存在死循环的无限递归错误。观察该界面的集成情况,发现为了Tab页实现,该界面在显示时,被两个ActivityGroup嵌套,增加了6层显式父layout。而因为该界面复杂,设计时,list_header view的layout深度为5层,而list_item的深度则为6层。原因定位为界面嵌套层次过深。

二、解决方法
解决嵌套过深的基本思路是在实现同样的效果的前提下减少界面的layout深度。在我将list_header view layout深度由5层精简为3层,list_item 深度由6层减少为4层后,该错误被解决。具体的方法有:
1) 多用RelativeLayout的各种布局属性来完成布局,而避免过多使用嵌套layout来实现。
2) 占位或填充剩余空间的元素多用View,避免使用layout等viewGroup。

总结下,从实际的数据来看,设计中layout嵌套深度超过10层,就应该考虑下优化了,否则就会出现java.lang.StackOverflowError的crash了。

三、错误详细日志
E/AndroidRuntime( 5196): FATAL EXCEPTION: main
E/AndroidRuntime( 5196): java.lang.StackOverflowError
E/AndroidRuntime( 5196): at android.graphics.Paint.measureText(Paint.java:1057)
E/AndroidRuntime( 5196): at android.text.Styled.drawDirectionalRun(Styled.java:267)
E/AndroidRuntime( 5196): at android.text.Styled.measureText(Styled.java:430)
E/AndroidRuntime( 5196): at android.text.Layout.measureText(Layout.java:1655)
E/AndroidRuntime( 5196): at android.text.Layout.getLineMax(Layout.java:689)
E/AndroidRuntime( 5196): at android.text.Layout.draw(Layout.java:340)
E/AndroidRuntime( 5196): at android.widget.TextView.onDraw(TextView.java:4050)
E/AndroidRuntime( 5196): at android.view.View.draw(View.java:6740)
E/AndroidRuntime( 5196): at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
E/AndroidRuntime( 5196): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
E/AndroidRuntime( 5196): at android.view.View.draw(View.java:6743)
E/AndroidRuntime( 5196): at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
E/AndroidRuntime( 5196): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
E/AndroidRuntime( 5196): at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
E/AndroidRuntime( 5196): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
E/AndroidRuntime( 5196): at android.view.View.draw(View.java:6743)
E/AndroidRuntime( 5196): at android.view.View.buildDrawingCache(View.java:6502)
E/AndroidRuntime( 5196): at android.view.View.getDrawingCache(View.java:6288)
E/AndroidRuntime( 5196): at android.view.ViewGroup.drawChild(ViewGroup.java:1565)
E/AndroidRuntime( 5196): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
E/AndroidRuntime( 5196): at android.widget.AbsListView.dispatchDraw(AbsListView.java:1365)
E/AndroidRuntime( 5196): at android.widget.ListView.dispatchDraw(ListView.java:3046)
E/AndroidRuntime( 5196): at android.view.View.draw(View.java:6846)
E/AndroidRuntime( 5196): at android.widget.AbsListView.draw(AbsListView.java:2257)
E/AndroidRuntime( 5196): at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
E/AndroidRuntime( 5196): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
E/AndroidRuntime( 5196): at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
E/AndroidRuntime( 5196): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
E/AndroidRuntime( 5196): at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
E/AndroidRuntime( 5196): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
E/AndroidRuntime( 5196): at android.view.View.draw(View.java:6743)
E/AndroidRuntime( 5196): at android.widget.FrameLayout.draw(FrameLayout.java:352)
E/AndroidRuntime( 5196): at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
E/AndroidRuntime( 5196): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
E/AndroidRuntime( 5196): at android.view.View.draw(View.java:6743)
E/AndroidRuntime( 5196): at android.widget.FrameLayout.draw(FrameLayout.java:352)
E/AndroidRuntime( 5196): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1842)
E/AndroidRuntime( 5196): at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
E/AndroidRuntime( 5196): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
E/AndroidRuntime( 5196): at android.view.View.draw(View.java:6743)
E/AndroidRuntime( 5196): at android.widget.FrameLayout.draw(FrameLayout.java:352)
E/AndroidRuntime( 5196): at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
E/AndroidRuntime( 5196): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
E/AndroidRuntime( 5196): at android.view.View.draw(View.java:6743)
E/AndroidRuntime( 5196): at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
E/AndroidRuntime( 5196): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
E/AndroidRuntime( 5196): at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
E/AndroidRuntime( 5196): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
E/AndroidRuntime( 5196): at android.view.View.draw(View.java:6743)
E/AndroidRuntime( 5196): at android.widget.FrameLayout.draw(FrameLayout.java:352)
E/AndroidRuntime( 5196): at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
E/AndroidRuntime( 5196): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
E/AndroidRuntime( 5196): at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
E/AndroidRuntime( 5196): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
E/AndroidRuntime( 5196): at android.view.View.draw(View.java:6743)
E/AndroidRuntime( 5196): at android.widget.FrameLayout.draw(FrameLayout.java:352)
E/AndroidRuntime( 5196): at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
E/AndroidRuntime( 5196): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
E/AndroidRuntime( 5196): at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
E/AndroidRuntime( 5196): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
E/AndroidRuntime( 5196): at android.view.View.draw(View.java:6743)
E/AndroidRuntime( 5196): at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
E/AndroidRuntime( 5196): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
E/AndroidRuntime( 5196): at android.view.View.draw(View.java:6743)
E/AndroidRuntime( 5196): at android.widget.FrameLayout.draw(FrameLayout.java:352)
E/AndroidRuntime( 5196): at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
E/AndroidRuntime( 5196): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
E/AndroidRuntime( 5196): at android.view.View.draw(View.java:6743)
E/AndroidRuntime( 5196): at android.widget.FrameLayout.draw(FrameLayout.java:352)
E/AndroidRuntime( 5196): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1842)
E/AndroidRuntime( 5196): at android.view.ViewRoot.draw(ViewRoot.java:1407)
E/AndroidRuntime( 5196): at
W/ActivityManager( 71): Force finishing activity com.baidu.test/.CloudDemActivity
W/WindowManager( 71): No window to dispatch pointer action 1