Java 手势检测器未按预期运行

Java 手势检测器未按预期运行,java,android,swipe,gesture-recognition,Java,Android,Swipe,Gesture Recognition,我正在尝试使用此处找到的手势检测器:。这是我的代码: public class FullScreenGallery extends Activity { int plane; private static final int SWIPE_MIN_DISTANCE = 120; private static final int SWIPE_MAX_OFF_PATH = 250; private static final int SWIPE_THRESHOLD_VELOCITY = 200; pri

我正在尝试使用此处找到的手势检测器:。这是我的代码:

public class FullScreenGallery extends Activity {
int plane;
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;
ImageView image;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);    //To change body of overridden methods use File | Settings | File Templates.
    setContentView(R.layout.full_screengallery);
    final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);

    final ImageView image = (ImageView) findViewById(R.id.imagePlane);
    plane = sp.getInt("PLANE",0);
    // Gesture detection
    gestureDetector = new GestureDetector(this, new MyGestureDetector());
    gestureListener = new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            return gestureDetector.onTouchEvent(event);
        }
    };

    image.setOnTouchListener(gestureListener);
    Context context = this;



    }


}

class MyGestureDetector extends GestureDetector.SimpleOnGestureListener {
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        try {
            if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                return false;
            // right to left swipe
            if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {

                Toast.makeText(FullScreenGallery.this, "Left Swipe", Toast.LENGTH_SHORT).show();
                Log.d("NICK", "Left Swipe");
        switch(X){
        case 0:
            Log.d("NICK", "Left Swipe,1");
        break;

                }
            }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                Toast.makeText(FullScreenGallery.this, "Right Swipe", Toast.LENGTH_SHORT).show();
                Log.d("NICK", "Right Swipe");
                switch(X){
        case 0:
            Log.d("NICK", "Right Swipe,1");
        break;
                }
            }
        } catch (Exception e) {
            // nothing
            Log.d("NICK", "Exception");
        }
        return false;
    }

    @Override
    public boolean onDown(MotionEvent e) {
        return true;    //To change body of overridden methods use File | Settings | File Templates.
    }
}
以及运行时的日志输出:

01-02 16:02:28.059: DEBUG/NICK(454): Left Swipe
01-02 16:02:28.059: DEBUG/NICK(454): Left Swipe, 1
01-02 16:02:28.069: DEBUG/NICK(454): Exception
01-02 16:02:50.500: DEBUG/NICK(454): Left Swipe
01-02 16:02:50.500: DEBUG/NICK(454): Exception
01-02 16:02:52.362: DEBUG/NICK(454): Left Swipe
01-02 16:02:52.362: DEBUG/NICK(454): Exception
01-02 16:02:53.593: DEBUG/NICK(454): Right Swipe
01-02 16:02:53.593: DEBUG/NICK(454): Exception
01-02 16:02:54.715: DEBUG/NICK(454): Right Swipe
01-02 16:02:54.715: DEBUG/NICK(454): Exception
01-02 16:02:55.625: DEBUG/NICK(454): Right Swipe
01-02 16:02:55.625: DEBUG/NICK(454): Exception
01-02 16:02:56.336: DEBUG/NICK(454): Right Swipe
01-02 16:02:56.336: DEBUG/NICK(454): Right Swipe,1
01-02 16:02:56.336: DEBUG/NICK(454): Exception
01-02 16:02:57.177: DEBUG/NICK(454): Right Swipe
01-02 16:02:58.088: DEBUG/NICK(454): Right Swipe
01-02 16:02:58.959: DEBUG/NICK(454): Right Swipe

所以我知道我遇到了一个例外,但我不知道为什么,也不知道如何解决它,这样除了简单的祝酒词外,我还可以用刷子做点什么。有什么建议可以导致这种情况吗?

什么是异常?我不是100%确定,这是问题的一部分。在异常处理程序中,您可以记录
e.getMessage()
e.printStackTrace(),而不只是记录“异常”一词
而不是记录某些内容。它是这样说的:尝试在空对象引用上调用虚拟方法“void android.widget.ImageView.setImageResource(int)”。我正试图在刷卡时将imageview更改为不同的图像,但我似乎没有正确地执行此操作。