0

I was trying to use the onBackPressed() method to open an activity that I need and close the existing one but the application close unexpectedly; I put a try catch if I can see the error but the variable "e" comes null, the only thing that show me something is the log, also I try to search for a solution that shows the log but it seems that those solutions do not apply to my case.

Here is my code:

 @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();
    }
}

and here is the log:

 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)

A curious thing is if for example I change the class That I want to reopen I t works without any problem, but I need when the user press the back button to open that specific class. Any help will be appreciated.

Edit:

I am using a Motorola g 2nd generation with android version 4.4.4 and a Samsung galaxy tab 7.7 p6800 with android version 4.0.4 both show me the same error.

On thing happen when I remove the try catch clauses and I am debugging step by step, on the following line of code

ScheduleAppointment.this.startActivity(intent);

redirect to the ZygoteInit.java class and in this method is were it shows the error on the log:

/**
     * 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);
                }
            }
        }
    }

and more precisely in this part:

if (done) {
    peers.remove(index);
    fds.remove(index);
} 
Marcus
  • 6,697
  • 11
  • 46
  • 89
jugger
  • 1
  • 6

2 Answers2

0

This worked for me, I think that an issue located in onPause, onStop or onDestroy methods

@Override
public void onBackPressed() {
    try {
        Intent intent = new Intent(this, CreateAppointment.class);
        startActivity(intent);
        finish();
    }catch (Exception e){
        e.printStackTrace();
    }
    super.onBackPressed();
}
mes
  • 3,581
  • 29
  • 28
  • Hi @mes this is really strange because I been using this code in my whole application and only in this specific class have this unexpected error. Also try your solution but have the same error. – jugger Feb 26 '15 at 18:00
  • I created blank activities and it worked, need more code to find out the reason, ScheduleAppointment and CreateAppointment, and also what device is it and which Android version is installed? – mes Feb 26 '15 at 18:06
  • I edit the question and put more information. Hope this helps. – jugger Feb 26 '15 at 19:03
0

My idea is the following, you should use a different context to create your Activity, rather than the Activity that you're currently in. For example, application context.

<application
    android:name=".application.CustomApplication"

and

public class CustomApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        ApplicationHolder.INSTANCE.setCustomApplication(this);
    }
}

and

public enum ApplicationHolder {
    INSTANCE;

    private CustomApplication customApplication = null;

    public CustomApplication getCustomApplication() {
        return customApplication ;
    }

    public void setCustomApplication(CustomApplication customApplication ) {
        this.customApplication = customApplication ;
    }
}

and

    Intent intent = new Intent(ApplicationHolder.getCustomApplication(), CreateAppointment.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_REORDER_TO_FRONT);
    startActivity(intent);
    finish();

If the bug still occurs, then start other activity with startActivityForResult, and call finish() in its callback method onActivityResult().

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
  • Hi EpicPandaForce, but why if I change the Class it works? for example: @Override public void onBackPressed() { Intent intent = new Intent(ScheduleAppointment.this, MainMenu.class); ScheduleAppointment.this.startActivity(intent); ScheduleAppointment.this.finish(); } I have been using the same line of code on the whole application without any problem, only in this class, and calling this class shows me that error. – jugger Feb 27 '15 at 13:00
  • please try using `startActivityForResult`, and override `onActivityResult` with `super.onActivityResult(); this.finish()`. – EpicPandaForce Feb 27 '15 at 14:48
  • Hi @EpicPandaForce, well I cannot implement that way because they ask me to change the class, and with other classes it works as I have been using it in the whole application. – jugger Mar 12 '15 at 13:47
  • oh, okay. Well that's problematic. – EpicPandaForce Mar 12 '15 at 14:15