1

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@[email protected]@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 
3
  • What is the exception you are getting in the logs? Commented Jan 14, 2014 at 17:53
  • with uncaught exception (group=0xb1a44b90) 01-15 11:14:04.589: E/AndroidRuntime(1863): FATAL EXCEPTION: main 01-15 11:14:04.589: E/AndroidRuntime(1863): Process: com.Candy.teacher, PID: 1863 01-15 11:14:04.589: E/AndroidRuntime(1863): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Candy.teacher/com.Candy.teacher.MainActivity}: java.lang.NullPointerException Commented Jan 15, 2014 at 16:18
  • So check again my answer and my comment on that answer Commented Jan 15, 2014 at 18:54

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

4 Comments

What do I pass then to let my app be able to run?? Once this app starts working and actually runs, I'll start working on the fragments and use ViewPager instead... But for now I just need to be able to run the app
I cannot guarantee for the rest of the code, but try this: ActivitySwipeDetector swipe = new ActivitySwipeDetector(this, this);
Can you paste the new log after this change?
There is a null reference in com.Candy.teacher.MainActivity at line 20. You probably changed the name of your activities, so you should also update the code in your question
1
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.