在项目中遇到一个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.2try{mVersionCode = Integer.valueOf(android.os.Build.VERSION.SDK);Log.d(TAG, "sdk version=" + mVersionCode);} catch (Exception e) {e.printStackTrace();}//针对android1.6及以下的特殊处理 此为android的低版本bugif(mVersionCode <= 5){linearLayoutScrolLayout.setBaselineAligned(false);}


更多相关文章

  1. 幻灯片播放学习笔记
  2. android ACodec MediaCodec NuPlayer flow
  3. android 资源如何查找(AaptAssets)分析
  4. android导入第三方包(解决ClassNotFound错误)
  5. Android(安卓)SQLite数据库版本升级的管理实现
  6. Android(安卓)camera系统开发之IPC (四)
  7. Android(安卓)ViewTreeObserver简介
  8. C++调用Android函数弹出一个提示框中出现的问题总结
  9. Android(安卓)Studio程序无法加载到虚拟机解决方法

随机推荐

  1. Android(安卓)Services使用示例
  2. Android Notes 01 - Bound Services
  3. android 欢迎界面的淡入效果
  4. Android 2.3文档学习
  5. Android屏蔽home键的代码,咋摁也不响应的
  6. Android Dialog顶部显示+横向全屏+沉浸式
  7. android 利用Gson生成json数据
  8. Android cts测试命令
  9. tabhost相关报错
  10. android notification 事件