简体   繁体   中英

How to exit from a listener in Android

I've Googled a lot but I still didn't find any solution for my problem. I'm new to Android so sorry for a stupid question. Here is my code:

final Intent myIntent = new Intent(this.getBaseContext(), ShowActivity.class);
webView.setOnTouchListener(new SwipeListener(this)
{
    @Override
    public void onSwipeLeft()
    {
        articleID--;
        //System.out.println("SWIPE_LEFT");
        myIntent.putExtra("id", articleID);
        System.out.println(myIntent.toString());
        Swiped = true;
    }
    @Override
    public void onSwipeRight()
    {
        articleID++;
        //System.out.println("SWIPE_RIGHT");
        myIntent.putExtra("id", articleID);
        System.out.println(myIntent.toString());
        Swiped = true;
    }
});
if (Swiped == true)
    mContext.startActivity(myIntent);

So, the question is: why my new activity doesn't start? I mean it doesn't even get into if-condition. Seems like that because of listener it ignores any code below, doesn't it? How to exit from listener? Btw, i'm trying to start new activity on Swipe. Maybe I need to rework architecture? But i really don't want to =(

This is my SwipeListener class:

public class SwipeListener implements View.OnTouchListener {
// Gesture Detector which has onTouchEvent action
// Gesture detector requires custom gesture listener (Which declared below)
private GestureDetector gestureDetector;
// Basic Construction
public SwipeListener(Context c) {
    gestureDetector = new GestureDetector(c, new GestureListener());
}
// onTouch action. Returns ANY (click and release) event thanks to onTouchEvent of gestureDetector
public boolean onTouch(final View view, final MotionEvent motionEvent) {
    return gestureDetector.onTouchEvent(motionEvent);
}

// GestureListener for GestureDetector declaration.
private final class GestureListener extends GestureDetector.SimpleOnGestureListener {

    private static final int SWIPE_MIN_DISTANCE = 100; // Swipe minimal distance
    private static final int SWIPE_MIN_VELOCITY = 100; // swipe minimal speed

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    // Determines the fling velocity and then fires the appropriate swipe event accordingly
    // onFling MUST return false so don't touch it.
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        try {
            float diffY = e2.getY() - e1.getY(); // Y(vertical) Difference between release and press coordinates
            float diffX = e2.getX() - e1.getX(); // X(horizontal) Difference between release and press coordinates
            // Determine if swipe was in horizontal or in vertical align
            // Horizontal (left-right)
            if (Math.abs(diffX) > Math.abs(diffY)) {
                // Check if speed and distance are big enough
                if (Math.abs(diffX) > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_MIN_VELOCITY) {
                    if (diffX > 0) {
                        onSwipeRight();
                    } else {
                        onSwipeLeft();
                    }
                }
            // Vertical (up-down)
            } else {
                if (Math.abs(diffY) > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_MIN_VELOCITY) {
                    if (diffY > 0) {
                        onSwipeDown();
                    } else {
                        onSwipeUp();
                    }
                }
            }
        } catch (Exception exception) {
            System.out.println("YOU SHALL NOT SWIPE");
        }
        return false;
    }
}

public void onSwipeRight() {
    // U need to override this
}

public void onSwipeLeft() {
    // and this
}

public void onSwipeUp() {
    // and this one too
}

public void onSwipeDown() {
    // U know what to do
}

}

Here is a log:

01-09 19:21:36.564    7758-7758/com.weel.ur W/System.err﹕ java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Intent.getFlags()' on a null object reference
01-09 19:21:36.565    7758-7758/com.weel.ur W/System.err﹕ at android.app.ContextImpl.startActivity(ContextImpl.java:1231)
01-09 19:21:36.565    7758-7758/com.weel.ur W/System.err﹕ at android.app.ContextImpl.startActivity(ContextImpl.java:1219)
01-09 19:21:36.565    7758-7758/com.weel.ur W/System.err﹕ at com.weel.ur.ShowActivity.startAct(ShowActivity.java:755)
01-09 19:21:36.565    7758-7758/com.weel.ur W/System.err﹕ at com.weel.ur.ShowActivity$2.onSwipeLeft(ShowActivity.java:347)
01-09 19:21:36.565    7758-7758/com.weel.ur W/System.err﹕ at com.weel.ur.calligraphy.SwipeListener$GestureListener.onFling(SwipeListener.java:51)
01-09 19:21:36.565    7758-7758/com.weel.ur W/System.err﹕ at android.view.GestureDetector.onTouchEvent(GestureDetector.java:610)
01-09 19:21:36.565    7758-7758/com.weel.ur W/System.err﹕ at com.weel.ur.calligraphy.SwipeListener.onTouch(SwipeListener.java:22)
01-09 19:21:36.565    7758-7758/com.weel.ur W/System.err﹕ at android.view.View.dispatchTouchEvent(View.java:8382)
01-09 19:21:36.565    7758-7758/com.weel.ur W/System.err﹕ at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2424)
01-09 19:21:36.565    7758-7758/com.weel.ur W/System.err﹕ at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2158)
01-09 19:21:36.565    7758-7758/com.weel.ur W/System.err﹕ at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
01-09 19:21:36.565    7758-7758/com.weel.ur W/System.err﹕ at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
01-09 19:21:36.565    7758-7758/com.weel.ur W/System.err﹕ at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
01-09 19:21:36.565    7758-7758/com.weel.ur W/System.err﹕ at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
01-09 19:21:36.565    7758-7758/com.weel.ur W/System.err﹕ at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
01-09 19:21:36.565    7758-7758/com.weel.ur W/System.err﹕ at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
01-09 19:21:36.565    7758-7758/com.weel.ur W/System.err﹕ at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
01-09 19:21:36.565    7758-7758/com.weel.ur W/System.err﹕ at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
01-09 19:21:36.565    7758-7758/com.weel.ur W/System.err﹕ at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
01-09 19:21:36.565    7758-7758/com.weel.ur W/System.err﹕ at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
01-09 19:21:36.565    7758-7758/com.weel.ur W/System.err﹕ at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2314)
01-09 19:21:36.565    7758-7758/com.weel.ur W/System.err﹕ at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1692)
01-09 19:21:36.565    7758-7758/com.weel.ur W/System.err﹕ at android.app.Activity.dispatchTouchEvent(Activity.java:2739)
01-09 19:21:36.565    7758-7758/com.weel.ur W/System.err﹕ at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.dispatchTouchEvent(ActionBarActivityDelegateICS.java:268)
01-09 19:21:36.565    7758-7758/com.weel.ur W/System.err﹕ at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2275)
01-09 19:21:36.565    7758-7758/com.weel.ur W/System.err﹕ at android.view.View.dispatchPointerEvent(View.java:8578)
01-09 19:21:36.566    7758-7758/com.weel.ur W/System.err﹕ at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4021)
01-09 19:21:36.566    7758-7758/com.weel.ur W/System.err﹕ at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3887)
01-09 19:21:36.566    7758-7758/com.weel.ur W/System.err﹕ at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449)
01-09 19:21:36.566    7758-7758/com.weel.ur W/System.err﹕ at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
01-09 19:21:36.566    7758-7758/com.weel.ur W/System.err﹕ at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3468)
01-09 19:21:36.566    7758-7758/com.weel.ur W/System.err﹕ at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3578)
01-09 19:21:36.566    7758-7758/com.weel.ur W/System.err﹕ at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3476)
01-09 19:21:36.566    7758-7758/com.weel.ur W/System.err﹕ at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3635)
01-09 19:21:36.566    7758-7758/com.weel.ur W/System.err﹕ at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449)
01-09 19:21:36.566    7758-7758/com.weel.ur W/System.err﹕ at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
01-09 19:21:36.566    7758-7758/com.weel.ur W/System.err﹕ at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3468)
01-09 19:21:36.566    7758-7758/com.weel.ur W/System.err﹕ at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3476)
01-09 19:21:36.566    7758-7758/com.weel.ur W/System.err﹕ at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449)
01-09 19:21:36.566    7758-7758/com.weel.ur W/System.err﹕ at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5701)
01-09 19:21:36.566    7758-7758/com.weel.ur W/System.err﹕ at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5675)
01-09 19:21:36.566    7758-7758/com.weel.ur W/System.err﹕ at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5646)
01-09 19:21:36.566    7758-7758/com.weel.ur W/System.err﹕ at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5791)
01-09 19:21:36.566    7758-7758/com.weel.ur W/System.err﹕ at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
01-09 19:21:36.566    7758-7758/com.weel.ur W/System.err﹕ at android.os.MessageQueue.nativePollOnce(Native Method)
01-09 19:21:36.566    7758-7758/com.weel.ur W/System.err﹕ at android.os.MessageQueue.next(MessageQueue.java:143)
01-09 19:21:36.566    7758-7758/com.weel.ur W/System.err﹕ at android.os.Looper.loop(Looper.java:122)
01-09 19:21:36.566    7758-7758/com.weel.ur W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5221)
01-09 19:21:36.566    7758-7758/com.weel.ur W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
01-09 19:21:36.566    7758-7758/com.weel.ur W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372)
01-09 19:21:36.567    7758-7758/com.weel.ur W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
01-09 19:21:36.567    7758-7758/com.weel.ur W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

You must have set default value for Swiped to false. Because you are starting activity outside of the listener its is called even before you get a chance to touch the screen. When onTouchListener is called value of Swiped is changed but you are not starting the activity.

    webView.setOnTouchListener(new SwipeListener(this)
    {
        @Override
        public void onSwipeLeft()
        {
            articleID--;
            //System.out.println("SWIPE_LEFT");
            Intent myIntent = new Intent(ShowActivity.this, ShowActivity.class);
            myIntent.putExtra("id", articleID);
            System.out.println(myIntent.toString());
            ShowActivity.this.startActivity(myIntent);
        }
        @Override
        public void onSwipeRight()
        {
            articleID++;
            //System.out.println("SWIPE_RIGHT");
            Intent myIntent = new Intent(ShowActivity.this, ShowActivity.class);
            myIntent.putExtra("id", articleID);
            System.out.println(myIntent.toString());
            Swiped = true;
            ShowActivity.this.startActivity(myIntent);
        }
    });

Also, you might want to add a condition before articleID-- and articleID++ . This will help you when you reach to first and last article in the queue. If so Swiped should be set to false or you can reset the articleID

You have confused the flow of your program. Setting a listener does not pause the program, and ending an in-listener method does not return to the method that created the listener. When you set a listener, you only set the listener , without running it. And then your program moves on.

This is what your program is dong:

  1. It sets the SwipeListener for webView.
  2. It asks if (Swiped == true) . But Swiped is false because listener is never automatically called upon setting, so activity is not started.
  3. It moves on.

And when a swipe happens, your program runs only what was defined in the listener . mContext.startActivity(myIntent); doesn't exist either in onSwipeLeft() or in onSwipeRight() methods. They just putExtra , then println , and then set Swipe to true. And then it moves on with user interface which has just called one of your onSwipe methods. You must insert mContext.startActivity(myIntent); in both of your listener methods.

You should change your code to this:

webView.setOnTouchListener(new SwipeListener(this)
{
    @Override
    public void onSwipeLeft()
    {
        articleID--;
        //System.out.println("SWIPE_LEFT");
        myIntent.putExtra("id", articleID);
        System.out.println(myIntent.toString());
        mContext.startActivity(myIntent);
    }
    @Override
    public void onSwipeRight()
    {
        articleID++;
        //System.out.println("SWIPE_RIGHT");
        myIntent.putExtra("id", articleID);
        System.out.println(myIntent.toString());
        mContext.startActivity(myIntent);
    }
});

UPDATE:

On the new problem, it seems like your myIntent turns out to be null somehow. It might be because of code-changing during attempts to fix it. Anyway, this update will probably not fail you. Make sure you do exactly as I describe.

webView.setOnTouchListener(new SwipeListener(this)
{
    @Override
    public void onSwipeLeft()
    {
        Intent myIntent = new Intent(mContext.getBaseContext(), ShowActivity.class);
        articleID--;
        myIntent.putExtra("id", articleID);
        System.out.println(myIntent.toString());
        mContext.startActivity(myIntent);
    }
    @Override
    public void onSwipeRight()
    {
        Intent myIntent = new Intent(mContext.getBaseContext(), ShowActivity.class);
        articleID++;
        myIntent.putExtra("id", articleID);
        System.out.println(myIntent.toString());
        mContext.startActivity(myIntent);
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM