gpt4 book ai didi

android - 如何在 android 中从一个 Activity 滑动到另一个 Activity ?

转载 作者:行者123 更新时间:2023-11-29 16:04:00 31 4
gpt4 key购买 nike

我有 2 个 Activity ,MainActivity 和 Advanced .....我想在从右向左滑动时打开 Advanced,在从左向右滑动时打开 Advanced to Main ....问题是,使用下面的代码...

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

}
}

这是我给高级 Activity 的编码.....我在主要 Activity 中给出了类似的代码

我的类(class)代码是...

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

最佳答案

您很可能从 ActivitySwipeDetector 的构造函数的第三行得到一个 NullPointerException:为什么要在Advanced 中的下一行?

ActivitySwipeDetector swipe = new ActivitySwipeDetector(null, this);

无论如何,要实现流畅的滑动,我觉得你应该先看这里:ViewPager tutorial , ViewPager . ViewPager 使用 Fragment,即描述部分 UI 或某些行为的应用程序 fragment ,它们被放入 Activity 中。 ViewPager 允许您创建屏幕幻灯片,每个幻灯片都是一个 Fragment 而不是 Activity

迁移很容易:因为 Fragment 的生命周期类似于 Activity 的,您只需将代码复制并粘贴到 Fragments 你要编码了。请参阅:Fragment tutorial

关于android - 如何在 android 中从一个 Activity 滑动到另一个 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21120339/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com