^_^ 真是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();
。。。