^_^ 真是Android Framework的BUG

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.