-1

I am aware of NullPointerException.

The reason behind this question was to clear the logic against gesture events.

Can somebody add the swipe gesture functionality in https://github.com/MikeOrtiz/TouchImageView to demonstrate the swipe left/right functionality?

I am using code of TouchImageView to enable the extended ImageView functionality of android application.

I want to capture Swipe gesture events and push the events to Main Activity to change the image manually.

following is the code from TouchImageView class

   private class GestureListener extends GestureDetector.SimpleOnGestureListener {

            private static final int SWIPE_THRESHOLD = 100;
            private static final int SWIPE_VELOCITY_THRESHOLD = 100;


            @Override
            public boolean onSingleTapConfirmed(MotionEvent e) {
                if (doubleTapListener != null) {
                    return doubleTapListener.onSingleTapConfirmed(e);
                }
                return performClick();
            }

            @Override
            public void onLongPress(MotionEvent e) {
                performLongClick();
            }

            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                if (fling != null) {
                    //
                    // If a previous fling is still active, it should be cancelled so that two flings
                    // are not run simultaenously.
                    //
                    fling.cancelFling();
                }

                try {
                    float diffY = e2.getY() - e1.getY();
                    float diffX = e2.getX() - e1.getX();
                    if (Math.abs(diffX) > Math.abs(diffY)) {
                        if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                            if (diffX > 0) {
                                onSwipeRight();
                            } else {
                                onSwipeLeft();
                            }
                            return true;
                        }
                    } else {
                        if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                            if (diffY > 0) {
                                onSwipeDown();
                            } else {
                                onSwipeUp();
                            }

                            return true;
                        }
                    }
                } catch (Exception exception) {
                    exception.printStackTrace();
                    return false;
                }

                fling = new Fling((int) velocityX, (int) velocityY);
                compatPostOnAnimation(fling);
                return super.onFling(e1, e2, velocityX, velocityY);
            }

            @Override
            public boolean onDoubleTap(MotionEvent e) {
                boolean consumed = false;
                if (doubleTapListener != null) {
                    consumed = doubleTapListener.onDoubleTap(e);
                }
                if (state == State.NONE) {
                    float targetZoom = (normalizedScale == minScale) ? maxScale : minScale;
                    DoubleTapZoom doubleTap = new DoubleTapZoom(targetZoom, e.getX(), e.getY(), false);
                    compatPostOnAnimation(doubleTap);
                    consumed = true;
                }
                return consumed;
            }

            @Override
            public boolean onDoubleTapEvent(MotionEvent e) {
                if (doubleTapListener != null) {
                    return doubleTapListener.onDoubleTapEvent(e);
                }
                return false;
            }

            public void onSwipeRight() {
// Problem Here. touchImageViewListener automatically sets to null
                touchImageViewListener.onSwipeRight();
            }

            public void onSwipeLeft() {
// Problem Here. touchImageViewListener automatically sets to null
                touchImageViewListener.onSwipeLeft();
            }

            public void onSwipeUp() {
            }

            public void onSwipeDown() {
            }
        }
    public interface OnTouchImageViewListener {
            public void onSwipeRight();
            public void onSwipeLeft();
            public void onMove();
        }

Here is the Main Activity

public class MainActivity extends ActionBarActivity implements TouchImageView.OnTouchImageViewListener {

private TouchImageView imageView;
@Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher1);
        imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {

            @Override
            public View makeView() {
                imageView = new TouchImageView(getApplicationContext());

                imageView.setMaxZoom(2F);
                imageView.setLayoutParams(new ImageSwitcher.LayoutParams(ImageSwitcher.LayoutParams.FILL_PARENT, ImageSwitcher.LayoutParams.FILL_PARENT));
                return imageView;
            }

        });

        imageView.setOnTouchImageViewListener(this);
    }

 @Override
    public void onSwipeRight() {
        this.switchImage(NavigationDirection.BACK);
        Toast.makeText(this, "Right", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onSwipeLeft() {
        this.switchImage(NavigationDirection.NEXT);
        Toast.makeText(this, "Left", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onMove() {

    }

I am getting NullPointerException in FlingMethod. It only works for 1st Swipe gesture. after that I am getting NullPointerException in logcat

Mohsan
  • 2,435
  • 5
  • 46
  • 59
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – njzk2 Feb 03 '15 at 19:27
  • Run it in debugging mode and keep track of the variables, you'll soon find out which variable is null and causing the issue. – IntegralOfTan Feb 03 '15 at 19:32

1 Answers1

0

I found the answer

   public class MainActivity extends ActionBarActivity implements TouchImageView.OnTouchImageViewListener {

    private TouchImageView imageView;
    @Override
        protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher1);
            imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {

                @Override
                public View makeView() {
                    imageView = new TouchImageView(getApplicationContext());

// Setting listener inside makeView function fixed the issue
                    imageView.setOnTouchImageViewListener(MainActivity.this);

                    imageView.setMaxZoom(2F);
                    imageView.setLayoutParams(new ImageSwitcher.LayoutParams(ImageSwitcher.LayoutParams.FILL_PARENT, ImageSwitcher.LayoutParams.FILL_PARENT));
                    return imageView;
                }

        });


    }
}
Mohsan
  • 2,435
  • 5
  • 46
  • 59