Question

I have 2 activities, MainActivity and Advanced ..... I want to open Advanced when Swiping from Right to left and Advanced to Main on swiping from Left to right.... The problem is, using the following code...

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.RelativeLayout;

public class Advanced extends Activity implements SwipeInterface {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_advanced);
    ActivitySwipeDetector swipe = new ActivitySwipeDetector(null, this);
    RelativeLayout swipe_layout = (RelativeLayout) findViewById(R.layout.activity_advanced);
    swipe_layout.setOnTouchListener(swipe);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.advanced, menu);
    return true;
}

@Override
public void onLeftToRight(View v) {
    // TODO Auto-generated method stub
    Intent intent = new Intent(this, MainActivity.class);
    startActivity(intent);
}

@Override
public void onRightToLeft(View v) {
    // TODO Auto-generated method stub

}
}

This is the coding I gave to the Advanced activity ..... I gave similar code in the main activity

My Class code is ...

import android.content.Context;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;

public class ActivitySwipeDetector implements View.OnTouchListener {

static final String logTag = "ActivitySwipeDetector";
private SwipeInterface activity;
private float downX, downY;
private long timeDown;
private final float MIN_DISTANCE;
private final int VELOCITY;
private final float MAX_OFF_PATH;

public ActivitySwipeDetector(Context context, SwipeInterface activity){
    this.activity = activity;
    final ViewConfiguration vc = ViewConfiguration.get(context);
    DisplayMetrics dm = context.getResources().getDisplayMetrics();
    MIN_DISTANCE = vc.getScaledPagingTouchSlop() * dm.density;
    VELOCITY = vc.getScaledMinimumFlingVelocity();
    MAX_OFF_PATH = MIN_DISTANCE * 2;            
}

public void onRightToLeftSwipe(View v){
    Log.i(logTag, "RightToLeftSwipe!");
    activity.onRightToLeft(v);
}

public void onLeftToRightSwipe(View v){
    Log.i(logTag, "LeftToRightSwipe!");
    activity.onLeftToRight(v);
}

public boolean onTouch(View v, MotionEvent event) {
    switch(event.getAction()){
    case MotionEvent.ACTION_DOWN: {
        Log.d("onTouch", "ACTION_DOWN");
        timeDown = System.currentTimeMillis();
        downX = event.getX();
        downY = event.getY();
        return true;
    }
    case MotionEvent.ACTION_UP: {
        Log.d("onTouch", "ACTION_UP");
        long timeUp = System.currentTimeMillis();
        float upX = event.getX();
        float upY = event.getY();

        float deltaX = downX - upX;
        float absDeltaX = Math.abs(deltaX); 
        float deltaY = downY - upY;
        float absDeltaY = Math.abs(deltaY);

        long time = timeUp - timeDown;

        if (absDeltaY > MAX_OFF_PATH) {
            Log.i(logTag, String.format("absDeltaY=%.2f, MAX_OFF_PATH=%.2f", absDeltaY, MAX_OFF_PATH));
            return v.performClick();
        }

        final long M_SEC = 1000;
        if (absDeltaX > MIN_DISTANCE && absDeltaX > time * VELOCITY / M_SEC) {
            if(deltaX < 0) { this.onLeftToRightSwipe(v); return true; }
            if(deltaX > 0) { this.onRightToLeftSwipe(v); return true; }
        } else {
            Log.i(logTag, String.format("absDeltaX=%.2f, MIN_DISTANCE=%.2f, absDeltaX > MIN_DISTANCE=%b", absDeltaX, MIN_DISTANCE, (absDeltaX > MIN_DISTANCE)));
            Log.i(logTag, String.format("absDeltaX=%.2f, time=%d, VELOCITY=%d, time*VELOCITY/M_SEC=%d, absDeltaX > time * VELOCITY / M_SEC=%b", absDeltaX, time, VELOCITY, time * VELOCITY / M_SEC, (absDeltaX > time * VELOCITY / M_SEC)));
        }

    }
    }
    return false;

}

}

My Interface contains

import android.view.View;

public interface SwipeInterface {

public void onLeftToRight(View v);

public void onRightToLeft(View v);

}

The problem is that the app terminates without opening..... The message comes ... Unfortunately, MyApp has stopped...

Please help me fix the problem.. There are no errors which arise in the code...

My Logcat display is as follows:

02-16 15:58:55.842: E/Trace(25383): error opening trace file: No such file or directory (2)
02-16 15:58:55.843: D/jdwp(25383): sendBufferedRequest : len=0x39
02-16 15:58:55.864: D/dalvikvm(25383):     open_cached_dex_file : /data/app/com.Candy.teacher-1.apk /data/dalvik-cache/data@app@com.Candy.teacher-1.apk@classes.dex
02-16 15:58:55.875: D/ActivityThread(25383): BIND_APPLICATION handled : 0 / AppBindData  {appInfo=ApplicationInfo{41fada00 com.Candy.teacher}}
02-16 15:58:55.958: D/AndroidRuntime(25383): Shutting down VM
02-16 15:58:55.958: W/dalvikvm(25383): threadid=1: thread exiting with uncaught exception (group=0x4179d908)
02-16 15:58:55.970: E/AndroidRuntime(25383): FATAL EXCEPTION: main
02-16 15:58:55.970: E/AndroidRuntime(25383): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Candy.teacher/com.Candy.teacher.MainActivity}: java.lang.NullPointerException
02-16 15:58:55.970: E/AndroidRuntime(25383):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
02-16 15:58:55.970: E/AndroidRuntime(25383):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2211)
02-16 15:58:55.970: E/AndroidRuntime(25383):    at android.app.ActivityThread.access$600(ActivityThread.java:149)
02-16 15:58:55.970: E/AndroidRuntime(25383):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1300)
02-16 15:58:55.970: E/AndroidRuntime(25383):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-16 15:58:55.970: E/AndroidRuntime(25383):    at android.os.Looper.loop(Looper.java:153)
02-16 15:58:55.970: E/AndroidRuntime(25383):    at android.app.ActivityThread.main(ActivityThread.java:4994)
02-16 15:58:55.970: E/AndroidRuntime(25383):    at java.lang.reflect.Method.invokeNative(Native Method)
02-16 15:58:55.970: E/AndroidRuntime(25383):    at java.lang.reflect.Method.invoke(Method.java:511)
02-16 15:58:55.970: E/AndroidRuntime(25383):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
02-16 15:58:55.970: E/AndroidRuntime(25383):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
02-16 15:58:55.970: E/AndroidRuntime(25383):    at dalvik.system.NativeStart.main(Native Method)
02-16 15:58:55.970: E/AndroidRuntime(25383): Caused by: java.lang.NullPointerException
02-16 15:58:55.970: E/AndroidRuntime(25383):    at com.Candy.teacher.MainActivity.onCreate(MainActivity.java:20)
02-16 15:58:55.970: E/AndroidRuntime(25383):    at android.app.Activity.performCreate(Activity.java:5023)
02-16 15:58:55.970: E/AndroidRuntime(25383):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-16 15:58:55.970: E/AndroidRuntime(25383):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
02-16 15:58:55.970: E/AndroidRuntime(25383):    ... 11 more
02-16 15:58:58.325: I/Process(25383): Sending signal. PID: 25383 SIG: 9
Was it helpful?

Solution 2

RelativeLayout swipe_layout = (RelativeLayout) findViewById(R.layout.activity_advanced);
swipe_layout.setOnTouchListener(swipe);

You won't find anything with findViewById() when passed in a layout identifier. A null is returned and invoking setONTouchListener() on it causes the NPE.

Pass in R.id.your_relativelayout_id where your_relativelayout_id is in fact a RelativeLayout in layout/activity_advanced.xml.

OTHER TIPS

You most likely get a NullPointerException from the third line in the constructor of ActivitySwipeDetector: why are you passing in a null Context at the following line in Advanced?

ActivitySwipeDetector swipe = new ActivitySwipeDetector(null, this);

Anyway, to achieve smooth swipes, I think you should look here first: ViewPager tutorial,ViewPager. A ViewPager uses Fragment's, pieces of application that describe parts of UI or some behaviour and that are put in an Activity. The ViewPager lets you create screen slides, each one being a Fragment rather than an Activity.

The migration is easy: since Fragment's lifecycle is similar to Activity's, you can just copy and paste your code into the Fragments you're going to code up. See: Fragment tutorial

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top