我试图使用onBackPressed()
方法打开我需要的活动并关闭现有活动,但应用程序意外关闭;如果我能看到错误但变量" e
"来null
,唯一能给我看的东西就是日志,我也尝试搜索显示日志的解决方案,但似乎这些解决方案并不适用于我的情况。
这是我的代码:
@Override
public void onBackPressed() {
try {
Intent intent = new Intent(ScheduleAppointment.this, CreateAppointment.class);
ScheduleAppointment.this.startActivity(intent);
ScheduleAppointment.this.finish();
}catch (Exception e){
e.printStackTrace();
}
}
这是日志:
02-26 12:21:44.062 2385-2385/com.saludtotal.beqs.saludtotalafiliados E/InputEventSender﹕ Exception dispatching finished signal.
02-26 12:21:44.063 2385-2385/com.saludtotal.beqs.saludtotalafiliados E/MessageQueue-JNI﹕ Exception in MessageQueue callback: handleReceiveCallback
02-26 12:21:44.114 2385-2385/com.saludtotal.beqs.saludtotalafiliados E/MessageQueue-JNI﹕ java.lang.NullPointerException
at com.saludtotal.beqs.saludtotalafiliados.ScheduleAppointment.onBackPressed(ScheduleAppointment.java:222)
at android.app.Activity.onKeyUp(Activity.java:2201)
at android.view.KeyEvent.dispatch(KeyEvent.java:2676)
at android.app.Activity.dispatchKeyEvent(Activity.java:2431)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1962)
at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:3921)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3895)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3452)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3471)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3578)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3479)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3635)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3452)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3471)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3479)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3452)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3471)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3611)
at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3777)
at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2012)
at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1706)
at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1697)
at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:1989)
at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:138)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:5097)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
02-26 12:21:44.114 2385-2385/com.saludtotal.beqs.saludtotalafiliados D/AndroidRuntime﹕ Shutting down VM
02-26 12:21:44.115 2385-2385/com.saludtotal.beqs.saludtotalafiliados W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41d93d40)
02-26 12:21:44.139 2385-2385/com.saludtotal.beqs.saludtotalafiliados E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.saludtotal.beqs.saludtotalafiliados, PID: 2385
java.lang.NullPointerException
at com.saludtotal.beqs.saludtotalafiliados.ScheduleAppointment.onBackPressed(ScheduleAppointment.java:222)
at android.app.Activity.onKeyUp(Activity.java:2201)
at android.view.KeyEvent.dispatch(KeyEvent.java:2676)
at android.app.Activity.dispatchKeyEvent(Activity.java:2431)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1962)
at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:3921)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3895)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3452)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3471)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3578)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3479)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3635)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3452)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3471)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3479)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3452)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3471)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3611)
at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3777)
at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2012)
at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1706)
at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1697)
at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:1989)
at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:138)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:5097)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
一个奇怪的事情是,例如我改变了我想要重新打开的课我没有任何问题,但我需要当用户按下后退按钮打开该特定课程。任何帮助将不胜感激。
编辑:
我使用的是摩托罗拉第二代Android版本4.4.4和三星Galaxy标签7.7 p6800与Android版本4.0.4都显示我同样的错误。
当我删除try catch子句并且我正在逐步调试时,在下面的代码行中发生了事情
ScheduleAppointment.this.startActivity(intent);
重定向到ZygoteInit.java类,在这个方法中,它显示了日志中的错误:
/**
* Runs the zygote process's select loop. Accepts new connections as
* they happen, and reads commands from connections one spawn-request's
* worth at a time.
*
* @throws MethodAndArgsCaller in a child process when a main() should
* be executed.
*/
private static void runSelectLoop(String abiList) throws MethodAndArgsCaller {
ArrayList<FileDescriptor> fds = new ArrayList<FileDescriptor>();
ArrayList<ZygoteConnection> peers = new ArrayList<ZygoteConnection>();
FileDescriptor[] fdArray = new FileDescriptor[4];
fds.add(sServerSocket.getFileDescriptor());
peers.add(null);
int loopCount = GC_LOOP_COUNT;
while (true) {
int index;
/*
* Call gc() before we block in select().
* It's work that has to be done anyway, and it's better
* to avoid making every child do it. It will also
* madvise() any free memory as a side-effect.
*
* Don't call it every time, because walking the entire
* heap is a lot of overhead to free a few hundred bytes.
*/
if (loopCount <= 0) {
gc();
loopCount = GC_LOOP_COUNT;
} else {
loopCount--;
}
try {
fdArray = fds.toArray(fdArray);
index = selectReadable(fdArray);
} catch (IOException ex) {
throw new RuntimeException("Error in select()", ex);
}
if (index < 0) {
throw new RuntimeException("Error in select()");
} else if (index == 0) {
ZygoteConnection newPeer = acceptCommandPeer(abiList);
peers.add(newPeer);
fds.add(newPeer.getFileDescriptor());
} else {
boolean done;
done = peers.get(index).runOnce();
if (done) {
peers.remove(index);
fds.remove(index);
}
}
}
}
更确切地说是这部分:
if (done) {
peers.remove(index);
fds.remove(index);
}
答案 0 :(得分:0)
这对我有用,我认为一个问题位于onPause,onStop或onDestroy方法
@Override
public void onBackPressed() {
try {
Intent intent = new Intent(this, CreateAppointment.class);
startActivity(intent);
finish();
}catch (Exception e){
e.printStackTrace();
}
super.onBackPressed();
}
答案 1 :(得分:0)
我的想法如下,您应该使用不同的上下文来创建您的Activity,而不是您当前所在的Activity。例如,应用程序上下文。
<application
android:name=".application.CustomApplication"
和
public class CustomApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
ApplicationHolder.INSTANCE.setCustomApplication(this);
}
}
和
public enum ApplicationHolder {
INSTANCE;
private CustomApplication customApplication = null;
public CustomApplication getCustomApplication() {
return customApplication ;
}
public void setCustomApplication(CustomApplication customApplication ) {
this.customApplication = customApplication ;
}
}
和
Intent intent = new Intent(ApplicationHolder.getCustomApplication(), CreateAppointment.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
finish();
如果错误仍然存在,请使用startActivityForResult
启动其他活动,并在其回调方法finish()
中调用onActivityResult()
。