我有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
}
}
这是我对高级活动的编码.....我在主要活动中给出了类似的代码
我的班级代码是......
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@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
答案 0 :(得分:3)
您最有可能从NullPointerException
的构造函数中的第三行获得ActivitySwipeDetector
:为什么要在Context
中的以下行传入空Advanced
?
ActivitySwipeDetector swipe = new ActivitySwipeDetector(null, this);
无论如何,为了实现顺畅的滑动,我认为你应该首先看一下:ViewPager tutorial,ViewPager。 ViewPager
使用Fragment
,描述部分UI或某些行为以及放在Activity
中的应用程序。 ViewPager
可让您创建屏幕幻灯片,每个幻灯片都是Fragment
而不是Activity
。
迁移很简单:由于Fragment
的生命周期类似于Activity
,因此您只需将代码复制并粘贴到要编码的Fragment
中即可起来。请参阅:Fragment tutorial
答案 1 :(得分:1)
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
{/ 1}}。