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
文章二:
问题:StackOverflowError
在HTC或者摩托罗拉的手机上测试出现 StackOverflowError 的错误。
06-12 10:28:31.750: E/AndroidRuntime(13995): FATAL EXCEPTION: main
06-12 10:28:31.750: E/AndroidRuntime(13995): java.lang.StackOverflowError
06-12 10:28:31.750: E/AndroidRuntime(13995): at android.text.Layout.getLineForOffset(Layout.java:1059)
06-12 10:28:31.750: E/AndroidRuntime(13995): at android.text.Layout$Ellipsizer.getChars(Layout.java:2270)
06-12 10:28:31.750: E/AndroidRuntime(13995): at android.text.TextUtils.getChars(TextUtils.java:70)
06-12 10:28:31.750: E/AndroidRuntime(13995): at android.graphics.Canvas.drawText(Canvas.java:1328)
06-12 10:28:31.750: E/AndroidRuntime(13995): at android.text.Layout.draw(Layout.java:651)
06-12 10:28:31.750: E/AndroidRuntime(13995): at android.widget.TextView.onDraw(TextView.java:4326)
06-12 10:28:31.750: E/AndroidRuntime(13995): at android.view.View.draw(View.java:7094)
06-12 10:28:31.750: E/AndroidRuntime(13995): at android.view.ViewGroup.drawChild(ViewGroup.java:1732)
06-12 10:28:31.750: E/AndroidRuntime(13995): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)
06-12 10:28:31.750: E/AndroidRuntime(13995): at android.view.View.draw(View.java:6988)
06-12 10:28:31.750: E/AndroidRuntime(13995): at android.view.ViewGroup.drawChild(ViewGroup.java:1732)
06-12 10:28:31.750: E/AndroidRuntime(13995): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)
06-12 10:28:31.750: E/AndroidRuntime(13995): at android.view.View.draw(View.java:6988)
06-12 10:28:31.750: E/AndroidRuntime(13995): at android.view.ViewGroup.drawChild(ViewGroup.java:1732)
06-12 10:28:31.750: E/AndroidRuntime(13995): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)
06-12 10:28:31.750: E/AndroidRuntime(13995): at android.view.ViewGroup.drawChild(ViewGroup.java:1730)
06-12 10:28:31.750: E/AndroidRuntime(13995): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)
06-12 10:28:31.750: E/AndroidRuntime(13995): at android.view.ViewGroup.drawChild(ViewGroup.java:1730)
06-12 10:28:31.750: E/AndroidRuntime(13995): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)
06-12 10:28:31.750: E/AndroidRuntime(13995): at android.view.ViewGroup.drawChild(ViewGroup.java:1730)
06-12 10:28:31.750: E/AndroidRuntime(13995): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)
06-12 10:28:31.750: E/AndroidRuntime(13995): at android.view.View.draw(View.java:6988)
从前面几行分析,跟文本控件有关。最后问题定位结果就是因为listView中textVIew嵌套太深所至。
我的listVIew布局层是这样的:
Listview--->RelativeLayout--->RelativeLayout--->RelativeLayout--->LinearLayout--->TextView
改为:
Listview--->RelativeLayout--->RelativeLayout--->RelativeLayout --->TextView
总结:尽可能的使用简单的布局和视图
如果一个窗口包含很多的视图,那么启动时间长、测量时间长、绘制时间长、布局时间长;如果视图树深度太深,会导致StackOverflowException异常,和用户界面反映会很慢很慢,如果在ListView 或者 GridView中使用则会更加严重。因此建议各位使用布局的时候,一定要选择合理的布局,复杂的堆砌即使解决了我们对复杂布局的处理,却很有可能降低我们程序的阅读效率,甚至抛出意想不到的异常。
解决的方法:
1.使用TextView的复合drawables,减少层次(该方法我们开发过程中屡试不爽!!!)
2.使用ViewStub延迟展开视图
3.使用<merge>合并视图
4.使用RelativeLayout减少层次
5.自定义布局