在项目中遇到一个bug,程序在android1.6上直接crash,但是在其他版本上均正常,错误日志如下:
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上却正常。
我们看一下2.x上这个getBaseline函数
@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,结果直接抛出异常了。
解决方法:
1. 就我目前在网上的调研,有人建议出现此类问题的时候,先addView再removeView,这样或许能解决问题,但是我觉得这样做很怪,让人觉得不可理解,平白无故的add一个又remove掉了,如果不写注释的话,别人可能会觉得代码写的有误。
2. 放弃了第一种方法,只好另外想办法,后来在android源码中找到了思路。请看LinearLayout中的measureHorizontal函数,其中有一段如下
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);
}