我遇到這個錯誤時避免ArrayIndexOutOfBoundsException異常:使用的onTouchEvent
09-13 18:20:50.183: ERROR/AndroidRuntime(300): java.lang.ArrayIndexOutOfBoundsException
09-13 18:20:50.183: ERROR/AndroidRuntime(300): at android.view.MotionEvent.getY(MotionEvent.java:792)
09-13 18:20:50.183: ERROR/AndroidRuntime(300): at android.widget.AbsListView.onTouchEvent(AbsListView.java:2040)
其中對於這裏的一個問題: http://code.google.com/p/android/issues/detail?id=10238#c6
我得到這個錯誤,當我試圖通過一個MotionEvent形成一個ListView到另一個。但是,只有當一個ListView具有onClickListeners視圖時纔會發生這種情況。
public boolean onTouch(View view, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN && !requestedFocus) {
view.requestFocus();
requestedFocus = true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
requestedFocus = false;
}
if(view == leftListView && view.isFocused()) {
rightListView.dispatchTouchEvent(event);
} else if(view == rightListView && view.isFocused()) {
leftListView.dispatchTouchEvent(event);
}
return false;
}
隨着上述一切的代碼工作正常,直到rightListView
動態加載視圖的那對他們onClickListeners。然後,此視圖傳遞給leftListView
的任何MotionEvent都會導致ArrayIndexOutOfBoundsException。事件從leftListView
到rightListView
按預期工作。
這似乎是因爲AbsListView的mActivePointerId == INVALID_POINTER,例如-1。我嘗試使用反射來改變這個值...但它沒有幫助。
所以,我的問題是,有沒有解決這個問題?
我的目標是同步兩個ListView的滾動,所以我願意廢棄這種方法,如果另一個是可行的。我曾嘗試使用ScrollListeners,但它們會導致其他問題。
在此先感謝!