2

嗨,我有这个 scrollView(特别是 TolerantScrollView.java)。它包含一个 MultiColumnListView 视图(一个 2 列列表视图,如带有适配器的 pinterest)问题是在某些手机上滚动视图没有问题,但在其他手机上它会因以下 NullPointerException 而崩溃。

FATAL EXCEPTION: main
java.lang.NullPointerException
   at android.widget.ScrollView$TouchMoveRunnable.touchDraw(ScrollView.java:566)
   at android.widget.ScrollView$TouchMoveRunnable.run(ScrollView.java:453)
   at android.os.Handler.handleCallback(Handler.java:605)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:137)
   at android.app.ActivityThread.main(ActivityThread.java:4477)
   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:788)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
   at dalvik.system.NativeStart.main(Native Method)

这是宽容的滚动视图类,我尝试用 try-catch 类包装 onTouchEvent,因为似乎错误发生在 onTouchEvent() 方法上,但 NullPointerException 仍然发生

public class TolerantScrollView extends ScrollView {

    private int mLastX;
    private int mLastY;

    private int distanceX;
    private int distanceY;

    private int mTouchSlop;

    public TolerantScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public TolerantScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public TolerantScrollView(Context context) {
        super(context);
        init();
    }

    private void init(){
        /**
         * define touch slop according to the display
         */
        final ViewConfiguration configuration = ViewConfiguration.get(getContext());
        mTouchSlop = configuration.getScaledTouchSlop();
    }
    /**
     * Processes every touch event through the {@link #onTouchEvent(MotionEvent)}
     * and intercepts if only we have restricted vertical scrolling.
     * <p>
     * 
     * {@inheritDoc}
     */
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        onTouchEvent(ev);
        int dy = 0;
        int dx = 0;
        switch(ev.getAction()){
        case MotionEvent.ACTION_DOWN:
            distanceX = 0;
            distanceY = 0;
            mLastX = (int) ev.getX();
            mLastY = (int) ev.getY();
            break;
        case MotionEvent.ACTION_MOVE:
            dx = Math.abs((int) (mLastX - ev.getX()));
            mLastX = (int) ev.getX();
            distanceX += dx;
            dy = Math.abs((int) (mLastY - ev.getY()));
            mLastY = (int) ev.getY();
            distanceY += dy;
            break;
        case MotionEvent.ACTION_CANCEL:
        case MotionEvent.ACTION_UP:
            // define whether we have vertical scrolling 
            // without horizontal one and intercept if so
            if (distanceY > mTouchSlop && distanceX < mTouchSlop){
                Log.d(VIEW_LOG_TAG, "intercepted");
                return true;
            }       
            distanceX = 0;
            distanceY = 0;
            break;
        }
        return false;
    }
}

为什么会发生 NullPointerException?在 LGOptimus G 上,错误不会发生,但在 LG Optimus It (L-05D) 上,滚动崩溃。

4

0 回答 0