在项目中遇到一个bug,程序在android1.6上直接crash,可是在其余版本上均正常,错误日志以下:html
04-07 17:02:53.512: E/AndroidRuntime(360): java.lang.RuntimeException: mBaselineAlignedChildIndex of LinearLayout set to an index that is out of bounds. 04-07 17:02:53.512: E/AndroidRuntime(360): at android.widget.LinearLayout.getBaseline(LinearLayout.java:151) 04-07 17:02:53.512: E/AndroidRuntime(360): at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:644) 04-07 17:02:53.512: E/AndroidRuntime(360): at android.widget.LinearLayout.onMeasure(LinearLayout.java:280) 04-07 17:02:53.512: E/AndroidRuntime(360): at android.view.View.measure(View.java:7703)
分析:这个错误意思是说,在LinearLayout中,measure函数里,当我要对其child进行基线对齐到第一个child的时候,发现个人内部没有child,结果基线对齐到第一个child(index为0)的时候,因为没法取到第一个child的信息,因此数组越界了。我当时在网上调研了下,发现有人说这个android1.x的一个bug,mBaselineAlignedChildIndex为LinearLayout的一个私有成员变量,在android1.x默认值是0,1.x以上默认值是-1,在google code上甚至有人举报了这个bug。这也就解释了为何程序跑在1.6上直接crash,而在2.x上却正常。java
咱们看一下2.x上这个getBaseline函数android
@Override public int getBaseline() { if (mBaselineAlignedChildIndex < 0) { return super.getBaseline(); } if (getChildCount() <= mBaselineAlignedChildIndex) { throw new RuntimeException("mBaselineAlignedChildIndex of LinearLayout " + "set to an index that is out of bounds."); }上述逻辑代表,若是mBaselineAlignedChildIndex = -1的话,直接return了,不然若是child数 <= mBaselineAlignedChildIndex,则访问越界,也就是文章开始提到的异常。从这段代码能够看出,当mBaselineAlignedChildIndex = -1的状况,在android2.x是不可能出现上述异常的,当时android1.6上mBaselineAlignedChildIndex居然等于0,结果直接抛出异常了。
解决方法:api
1. 就我目前在网上的调研,有人建议出现此类问题的时候,先addView再removeView,这样或许能解决问题,可是我以为这样作很怪,让人以为不可理解,无缘无故的add一个又remove掉了,若是不写注释的话,别人可能会以为代码写的有误。数组
2. 放弃了第一种方法,只好另外想办法,后来在android源码中找到了思路。请看LinearLayout中的measureHorizontal函数,其中有一段以下ide
if (widthMode == MeasureSpec.EXACTLY && lp.width == 0 && lp.weight > 0) {
// Optimization: don't bother measuring children who are going to use
// leftover space. These views will get measured again down below if
// there is any leftover space.
if (isExactly) {
mTotalLength += lp.leftMargin + lp.rightMargin;
} else {
final int totalLength = mTotalLength;
mTotalLength = Math.max(totalLength, totalLength +
lp.leftMargin + lp.rightMargin);
}
// Baseline alignment requires to measure widgets to obtain the
// baseline offset (in particular for TextViews). The following
// defeats the optimization mentioned above. Allow the child to
// use as much space as it wants because we can shrink things
// later (and re-measure).
if (baselineAligned) { final int freeSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); child.measure(freeSpec, freeSpec); }
}
对比出错的log,发现调用栈很一致,OnMeasure调用了measureHorizontal,measureHorizontal调用了child的measure。注意到上述代码的最后几句,if(baselineAligned)这一句很关键,这个变量就表示是否要进行基线对齐,其实这个基线对齐对布局的影响不大,通常来讲咱们不须要设置这个变量,我想既然是基线对齐的时候程序挂的,那为什么不取消基线对齐呢,恰好LinearLayout提供了setBaselineAligned这个函数,试着调用setBaselineAligned(false),发现问题解决了。函数
整理一下,判断一下,若是发现系统版本是1.x(基本只有1.6和1.5),则调用setBaselineAligned(false),其余api版本什么也不作。布局
int mVersionCode = 8;//default value = android 2.2 try { mVersionCode = Integer.valueOf(android.os.Build.VERSION.SDK); Log.d(TAG, "sdk version=" + mVersionCode); } catch (Exception e) { e.printStackTrace(); } //针对android1.6及如下的特殊处理 此为android的低版本bug if(mVersionCode <= 5) { linearLayoutScrolLayout.setBaselineAligned(false); }