现在的位置: 首页 > 编程技术 > 安卓开发 > 正文

真的遇到StackOverflowError

2015年09月10日 安卓开发 ⁄ 共 7692字 ⁄ 字号 真的遇到StackOverflowError已关闭评论 ⁄ 阅读 939 次

公司的app还要支持2.2-4.0的版本,说实话现在手头上去哪里搞一个2.2的手机,连genymotion最低的模拟器版本都2.3了,微信最新版也是4.0.3以上了。我们什么时候能任性一把(发发牢骚)。

app在4.0上运行正常,在2.3的模拟器上直接crash,爆出

08-27 10:25:58.456: E/AndroidRuntime(4618): FATAL EXCEPTION: main
08-27 10:25:58.456: E/AndroidRuntime(4618): java.lang.StackOverflowError
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.text.Styled.measureText(Styled.java:430)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.text.Layout.measureText(Layout.java:1655)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.text.Layout.getLineMax(Layout.java:689)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.text.Layout.draw(Layout.java:340)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.text.BoringLayout.draw(BoringLayout.java:365)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.widget.TextView.onDraw(TextView.java:4170)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.View.draw(View.java:6880)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.View.draw(View.java:6883)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.widget.AbsListView.dispatchDraw(AbsListView.java:1480)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.widget.ListView.dispatchDraw(ListView.java:3138)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.View.draw(View.java:6883)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.widget.AbsListView.draw(AbsListView.java:2584)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.View.draw(View.java:6883)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.widget.FrameLayout.draw(FrameLayout.java:357)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.View.draw(View.java:6883)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.widget.FrameLayout.draw(FrameLayout.java:357)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.View.draw(View.java:6883)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.widget.FrameLayout.draw(FrameLayout.java:357)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.View.draw(View.java:6883)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.widget.FrameLayout.draw(FrameLayout.java:357)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.View.draw(View.java:6883)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.widget.FrameLayout.draw(FrameLayout.java:357)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.View.draw(View.java:6883)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.widget.FrameLayout.draw(FrameLayout.java:357)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1862)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.ViewRoot.draw(ViewRoot.java:1522)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.ViewRoot.performTraversals(ViewRoot.java:1258)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
08-27 10:25:58.456: E/AndroidRuntime(4618): 	at and
08-27 10:25:58.460: W/ActivityManager(164):   Force finishing activity com.xxxx.xxxx/.activities.MainActivity

同事经过看得此景,大家一笑说这下应验了stackoverflow。

对于这个bug就是因为view嵌套太深而导致了,具体什么叫深呢?10层以上就可以叫做深了,到了15层就很有可能引发此bug。而对于安卓系统来说其UI线程的堆大小为:

  • API 3 (Android 1.5) = 8KB
  • API 4-10 (Android 1.6 - Android 2.3.7) = 12KB
  • API 14-17 (Android 4.0 - Android 4.2.2) = 16KB

所以我们要做的就是尽可能的减少套嵌的深度,像下面这种的要完全避免。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f4f4f4"
    android:orientation="vertical" >
    
    <FrameLayout
        android:id="@+id/xxx_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
     </FrameLayout>
    
</FrameLayout>

 

抱歉!评论已关闭.