有人提交的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();
。。。