我有2个活动,MainActivity和Advanced .....
我想在从右向左滑动时打开高级,从左向右滑动时打开高级到主...。
问题是,使用以下代码...
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
}
}
这是我给Advanced活动提供的编码.....我在main活动中给出了类似的代码
我的班级代码是...
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;
}
}
我的界面包含
import android.view.View;
public interface SwipeInterface {
public void onLeftToRight(View v);
public void onRightToLeft(View v);
}
问题是该应用程序终止而没有打开.....
消息来了...不幸的是,MyApp已停止...
请帮助我解决问题。
代码中没有出现任何错误...
我的Logcat显示如下:
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
最佳答案
RelativeLayout swipe_layout = (RelativeLayout) findViewById(R.layout.activity_advanced);
swipe_layout.setOnTouchListener(swipe);
传递
findViewById()
标识符时,找不到带有layout
的任何内容。返回一个null
,对其调用setONTouchListener()
会导致NPE。传入
R.id.your_relativelayout_id
,其中your_relativelayout_id
实际上是RelativeLayout
中的layout/activity_advanced.xml
。