0

我在修改 PullToRefresh 库时遇到问题

https://github.com/johannilsson/android-pulltorefresh/pull/40

它在 android 版本 2.x 中运行良好,但在 4.x 中崩溃(我没有 3.x 设备来检查是否有效)。

在 4.x 中,它在方法中失败:

 private void applyHeaderPadding(MotionEvent ev) {
    // Workaround for getPointerCount() which is unavailable in 1.5
    // (it's always 1 in 1.5)
    int pointerCount = 1;
    try {
        Method method = MotionEvent.class.getMethod("getPointerCount");
        pointerCount = (Integer)method.invoke(ev);
    } catch (NoSuchMethodException e) {
        pointerCount = 1;
    } catch (IllegalArgumentException e) {
        throw e;
    } catch (IllegalAccessException e) {
        System.err.println("unexpected " + e);
    } catch (InvocationTargetException e) {
        System.err.println("unexpected " + e);
    }

    for (int p = 0; p < pointerCount; p++) {
        if (mRefreshState == RELEASE_TO_REFRESH) {
            if (isVerticalFadingEdgeEnabled()) {
                setVerticalScrollBarEnabled(false);
            }

            int historicalY = (int) ev.getHistoricalY(p);

            // Calculate the padding to apply, we divide by 1.7 to
            // simulate a more resistant effect during pull.
            int topPadding = (int) (((historicalY - mLastMotionY)
                    - mRefreshViewHeight) / 1.7);

            mRefreshView.setPadding(
                    mRefreshView.getPaddingLeft(),
                    topPadding,
                    mRefreshView.getPaddingRight(),
                    mRefreshView.getPaddingBottom());
        }
    }
}

崩溃中的行是:

int historicalY = (int) ev.getHistoricalY(p);

有这个特例:

java.lang.IllegalArgumentException: historyPos out of range
at android.view.MotionEvent.nativeGetAxisValue(Native Method)
at android.view.MotionEvent.getHistoricalY(MotionEvent.java:2314)
at com.usualbike.cbike.PullToRefresh.applyHeaderPadding(PullToRefresh.java:241)
at com.usualbike.cbike.PullToRefresh.onTouchEvent(PullToRefresh.java:212)
at android.view.View.dispatchTouchEvent(View.java:5717)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1956)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1717)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1962)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1731)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1962)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1731)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1962)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1731)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1962)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1731)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1962)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1731)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1962)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1731)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1962)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1731)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1962)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1731)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2064)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1400)
at android.app.Activity.dispatchTouchEvent(Activity.java:2369)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2012)
at android.view.View.dispatchPointerEvent(View.java:5897)
at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3115)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2657)
at android.view.ViewRootImpl.processInputEvents(ViewRootImpl.java:1021)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2666)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4511)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
at dalvik.system.NativeStart.main(Native Method)

我该如何解决这个问题?

4

2 回答 2

1

我不确定,但尝试改变

int historicalY = (int) ev.getHistoricalY(p);

if(p>pointerCount)
{
int historicalY = (int) ev.getHistoricalY(p);
}
于 2013-01-11T09:28:54.970 回答
0

你可能想试试我的图书馆:https ://github.com/chrisbanes/Android-PullToRefresh

于 2013-01-11T10:11:46.283 回答