^_^ 真是Android Framework的BUG

有人提交的Bug描述: http://code.google.com/p/android/issues/detail?id=3484

现象:如果你将LinearLayout作为一个View添加到根目录中,但是这个LinearLayout没有子View的话,

运行的时候就会收到如下的error:

06-18 21:50:44.020: ERROR/AndroidRuntime(28605):
> java.lang.RuntimeException: mBaselineAlignedChildIndex of LinearLayout
> set to an index that is out of bounds.
> 06-18 21:50:44.020: ERROR/AndroidRuntime(28605): at
> android.widget.LinearLayout.getBaseline(LinearLayout.java:151)
> 06-18 21:50:44.020: ERROR/AndroidRuntime(28605): at
> android.widget.LinearLayout.measureHorizontal(LinearLayout.java:644)
> 06-18 21:50:44.020: ERROR/AndroidRuntime(28605): at
> android.widget.LinearLayout.onMeasure(LinearLayout.java:280)
> 06-18 21:50:44.020: ERROR/AndroidRuntime(28605): at
> android.view.View.measure(View.java:7712)
> 06-18 21:50:44.020: ERROR/AndroidRuntime(28605): at
> android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3044)
> 06-18 21:50:44.020: ERROR/AndroidRuntime(28605): at
> android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:888)
> 06-18 21:50:44.020: ERROR/AndroidRuntime(28605): at
> android.widget.LinearLayout.measureVertical(LinearLayout.java:350)
> 06-18 21:50:44.020: ERROR/AndroidRuntime(28605): at
> android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
> 06-18 21:50:44.020: ERROR/AndroidRuntime(28605): at
> android.view.View.measure(View.java:7712)
> 06-18 21:50:44.020: ERROR/AndroidRuntime(28605): at
> android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3044)
> 06-18 21:50:44.020: ERROR/AndroidRuntime(28605): at
> android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
> 06-18 21:50:44.020: ERROR/AndroidRuntime(28605): at
> android.view.View.measure(View.java:7712)
> 06-18 21:50:44.020: ERROR/AndroidRuntime(28605): at
> android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3044)
> 06-18 21:50:44.020: ERROR/AndroidRuntime(28605): at
> android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
> 06-18 21:50:44.020: ERROR/AndroidRuntime(28605): at
> android.view.View.measure(View.java:7712)
> 06-18 21:50:44.020: ERROR/AndroidRuntime(28605): at
> android.view.ViewRoot.performTraversals(ViewRoot.java:824)
> 06-18 21:50:44.020: ERROR/AndroidRuntime(28605): at
> android.view.ViewRoot.handleMessage(ViewRoot.java:1774)
> 06-18 21:50:44.020: ERROR/AndroidRuntime(28605): at
> android.os.Handler.dispatchMessage(Handler.java:99)
> 06-18 21:50:44.020: ERROR/AndroidRuntime(28605): at
> android.os.Looper.loop(Looper.java:123)
> 06-18 21:50:44.020: ERROR/AndroidRuntime(28605): at
> android.app.ActivityThread.main(ActivityThread.java:4321)
> 06-18 21:50:44.020: ERROR/AndroidRuntime(28605): at
> java.lang.reflect.Method.invokeNative(Native Method)
> 06-18 21:50:44.020: ERROR/AndroidRuntime(28605): at
> java.lang.reflect.Method.invoke(Method.java:521)
> 06-18 21:50:44.020: ERROR/AndroidRuntime(28605): at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
> 06-18 21:50:44.020: ERROR/AndroidRuntime(28605): at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
> 06-18 21:50:44.020: ERROR/AndroidRuntime(28605): at
> dalvik.system.NativeStart.main(Native Method)

这个错误是因为LinearLayout中的成员变量的默认值取错了,

 
  
/**
* If this layout is part of another layout that is baseline aligned,
* use the child at this index as the baseline.
*
* Note: this is orthogonal to {
@link #mBaselineAligned}, which is concerned
* with whether the children of this layout are baseline aligned.
*/
private int mBaselineAlignedChildIndex = 0 ;

2.0(估计中间的某个修订版已经修正了,但是具体版本不得而知了)之后的SDK就修正了这个问题:

 
  
/**
* If this layout is part of another layout that is baseline aligned,
* use the child at this index as the baseline.
*
* Note: this is orthogonal to {
@link #mBaselineAligned}, which is concerned
* with whether the children of this layout are baseline aligned.
*/
@ViewDebug.ExportedProperty(category
= " layout " )
private int mBaselineAlignedChildIndex = - 1 ;

:-),就一个默认值惹出的麻烦啊,一般这样用LinearLayout的可以直接View控件来代替即可。

而抛出异常的地方就是:

 
  
@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. " );
}

final View child = getChildAt(mBaselineAlignedChildIndex);
final int childBaseline = child.getBaseline();
。。。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值