基线函数android 1.6中LinearLayout getBaseline函数的一个bug

发一下牢骚和主题无关:

    在项目中碰到一个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);
		}

文章结束给大家分享下程序员的一些笑话语录: 这个世界上只有10种人:懂得二进制的和不懂得二进制的。

posted @ 2013-05-06 20:17  xinyuyuanm  阅读(579)  评论(0编辑  收藏  举报