0

我试图使用该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 的 Motorola g 第二代和带有 android 版本 4.0.4 的 Samsung Galaxy Tab 7.7 p6800 都向我显示相同的错误。

当我删除 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);
} 
4

2 回答 2

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();
}
于 2015-02-26T17:41:22.647 回答
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()

于 2015-02-27T09:01:37.580 回答