1. 程式人生 > >錯誤 java.lang.IllegalArgumentException: parameter must be a descendant of this view 的解決方案

錯誤 java.lang.IllegalArgumentException: parameter must be a descendant of this view 的解決方案

bug資訊:

10-16 17:27:20.250: D/AndroidRuntime(14662): Shutting down VM
10-16 17:27:20.250: W/dalvikvm(14662): threadid=1: thread exiting with uncaught exception (group=0x40a5e228)
10-16 17:27:20.250: E/AndroidRuntime(14662): FATAL EXCEPTION: main
10-16 17:27:20.250: E/AndroidRuntime(14662): java.lang.IllegalArgumentException: parameter must be a descendant of this view
10-16 17:27:20.250: E/AndroidRuntime(14662):  at android.view.ViewGroup.offsetRectBetweenParentAndChild(ViewGroup.java:4460)
10-16 17:27:20.250: E/AndroidRuntime(14662):  at android.view.ViewGroup.offsetDescendantRectToMyCoords(ViewGroup.java:4397)
10-16 17:27:20.250: E/AndroidRuntime(14662):  at android.widget.HorizontalScrollView.isWithinDeltaOfScreen(HorizontalScrollView.java:1054)
10-16 17:27:20.250: E/AndroidRuntime(14662):  at android.widget.HorizontalScrollView.onSizeChanged(HorizontalScrollView.java:1415)
10-16 17:27:20.250: E/AndroidRuntime(14662):  at android.view.View.setFrame(View.java:11442)
10-16 17:27:20.250: E/AndroidRuntime(14662):  at android.view.View.layout(View.java:11353)
10-16 17:27:20.250: E/AndroidRuntime(14662):  at android.view.ViewGroup.layout(ViewGroup.java:4531)
10-16 17:27:20.250: E/AndroidRuntime(14662):  at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
10-16 17:27:20.250: E/AndroidRuntime(14662):  at android.view.View.layout(View.java:11359)
10-16 17:27:20.250: E/AndroidRuntime(14662):  at android.view.ViewGroup.layout(ViewGroup.java:4531)
10-16 17:27:20.250: E/AndroidRuntime(14662):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
10-16 17:27:20.250: E/AndroidRuntime(14662):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
10-16 17:27:20.250: E/AndroidRuntime(14662):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
10-16 17:27:20.250: E/AndroidRuntime(14662):  at android.view.View.layout(View.java:11359)
10-16 17:27:20.250: E/AndroidRuntime(14662):  at android.view.ViewGroup.layout(ViewGroup.java:4531)
10-16 17:27:20.250: E/AndroidRuntime(14662):  at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
10-16 17:27:20.250: E/AndroidRuntime(14662):  at android.view.View.layout(View.java:11359)
10-16 17:27:20.250: E/AndroidRuntime(14662):  at android.view.ViewGroup.layout(ViewGroup.java:4531)
10-16 17:27:20.250: E/AndroidRuntime(14662):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1665)
10-16 17:27:20.250: E/AndroidRuntime(14662):  at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2695)
10-16 17:27:20.250: E/AndroidRuntime(14662):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-16 17:27:20.250: E/AndroidRuntime(14662):  at android.os.Looper.loop(Looper.java:154)
10-16 17:27:20.250: E/AndroidRuntime(14662):  at android.app.ActivityThread.main(ActivityThread.java:4974)
10-16 17:27:20.250: E/AndroidRuntime(14662):  at java.lang.reflect.Method.invokeNative(Native Method)
10-16 17:27:20.250: E/AndroidRuntime(14662):  at java.lang.reflect.Method.invoke(Method.java:511)
10-16 17:27:20.250: E/AndroidRuntime(14662):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-16 17:27:20.250: E/AndroidRuntime(14662):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-16 17:27:20.250: E/AndroidRuntime(14662):  at dalvik.system.NativeStart.main(Native Method)

場景描述:

        用listview寫的一個書架,點選超出一屏區域的書本進入閱讀模組,在返回時會丟擲如上崩潰資訊,若是未超出螢幕區域的書本則不會崩潰。

分析與解決:

       parameter must be a descendant of this view 是說這個parameter必須是這個view的子孫...

        因為我的listview外面巢狀的有一個HorizontalScrollView所以很有可能就是它們兩個衝突導致的,跟蹤檢視HorizontalScrollView的原始碼及分析日誌資訊,出現這個bug的前提條件:當listview向下滾動了再返回時會呼叫HorizontalScrollView的onSizeChanged()方法,在onSizeChanged()方法中呼叫了findfocus方法得到返回的介面中當前擁有焦點的view。從上面那句話話可以看出關鍵點就在這個view上,如果它是HorizontalScrollView 的子孫程式就不會崩潰了,所以讓程式返回時焦點直接落在某一指定view上就可以了。我這時是讓listview一直擁有焦點,覆寫了它的isFocused方法讓它一直返回true,問題就解決了!!