Android开发出现 StackOverflowError

问题: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.自定义布局

         详细见:http://www.cnblogs.com/felix-hua/archive/2012/01/06/2314963.html

 

         以上见解纯属个人理解,欢迎大家多多指教,共同学习,共同进步!

posted on 2012-06-13 08:42  沙翁  阅读(13735)  评论(3编辑  收藏  举报

导航