使用ScrollView的android NullPointerException

时间:2013-05-28 02:39:28

标签: android nullpointerexception android-scrollview

您好我有这个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)

这是容忍的scrollview类,我尝试用try-catch clase包装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)上滚动崩溃。

0 个答案:

没有答案