读书人

Android格局之java.lang.StackOverflo

发布时间: 2012-09-15 19:09:28 作者: rapoo

Android布局之java.lang.StackOverflowError

??????? 今天做项目的时候,忽然遇到了上面的错误,于是上网搜了下,已经有很多该类的文章或者疑惑了,但是跟自己android中遇到的问题并不是一样的。不过呢,总算从网上得到了一个结论:那就是要递归调用层次太深。

?? 于是呢,就进行查询自己的代码,仔细回想,刚刚似乎并没有动过java文件,而修改之处在于XML资源文件。终于经过再三的验证,发现问题在于——结构嵌套层次太深。或许说了半天,你没明白,下面就用我们程序员的语言——代码,来进行分析:

<?xml version="1.0" encoding="utf-8"?>?
<LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"?
??????????????android:layout_width="fill_parent"?
??????????????android:layout_height="fill_parent"?
??????????????android:orientation="horizontal">

? <RelativeLayout android:layout_width="wrap_content"

???????????? android:layout_height="fill_parent">

???<!-- <RelativeLayout????????????????????????????????????? ----1

?????????????? android:layout_width="200px"

?????????????? android:layout_height="fill_parent">

???? -->
????<com.china.MyButton?android:id="@+id/button1"?--------2
????????????android:layout_width="wrap_content"?
????????????android:layout_height="wrap_content"????????????
????????????android:text="@string/button_hint"?
????????????android:layout_weight="1"?
????????????/>?

???? <!-- </RelativeLayout> -->
???? <TextView android:id="@+id/tv"

?????????? ......

???????????? >

????? <TextView android:id="@+id/tv"

?????????? ......

???????????? >

???</RelativeLayout>
</LinearLayout>

请仔细看上面的布局,其中--2中的MyButton是继承自Button的一个自定义控件,其目前所处的结构位于LinearLayout下面的RelativeLayout里面,相当于两个ViewGroup里面,而其本身又继承自Button,若此时再加上一层结构RelativeLayout,就会出现Stackoverflowerror的错误。因此建议各位使用布局的时候,一定要选择合理的布局,复杂的堆砌即使解决了我们对复杂布局的处理,却很有可能降低我们程序的阅读效率,甚至抛出意想不到的异常。

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

读书人网 >Ruby Rails

热点推荐