后退按钮崩溃

时间:2017-11-30 21:41:36

标签: android google-maps android-studio

当我点击后退按钮时,我的应用程序崩溃了。我希望当用户单击后退按钮而不创建标记时,会弹出一个对话框,提示用户做出决定。但是,当发生这种情况时,标记m应该返回null,这是我所考虑的,这意味着应用程序上没有标记,但它仍然会崩溃。

@Override
public void onBackPressed() {
    super.onBackPressed();
    HabitEventController hec = new HabitEventController(this);

    if(m.getPosition() != null){
        hec.setHabitEventLocation(heID, m.getPosition());
   }

   if(m.getPosition() == null){
       new AlertDialog.Builder(this)
               .setTitle("Really Exit?")
               .setMessage("Are you sure you want to exit, without creating a marker?")
               .setNegativeButton(android.R.string.no, null)
               .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int whichButton) {
                       MapsActivity.super.onBackPressed();
                       dialog.dismiss();

                   }
               }).show();
   }
    finish();
}
LogCat


11-30 13:49:41.226 6145-6145/com.example.habitrack E/AndroidRuntime: FATAL EXCEPTION: main
                                                                 Process: com.example.habitrack, PID: 6145
                                                                 java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.model.LatLng com.google.android.gms.maps.model.Marker.getPosition()' on a null object reference
                                                                     at com.example.habitrack.MapsActivity.onBackPressed(MapsActivity.java:241)
                                                                     at android.app.Activity.onKeyUp(Activity.java:2965)
                                                                     at android.view.KeyEvent.dispatch(KeyEvent.java:2712)
                                                                     at android.app.Activity.dispatchKeyEvent(Activity.java:3257)
                                                                     at com.android.tools.profiler.support.event.WindowProfilerCallback.dispatchKeyEvent(WindowProfilerCallback.java:52)
                                                                     at com.android.internal.policy.DecorView.dispatchKeyEvent(DecorView.java:351)
                                                                     at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4714)
                                                                     at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4586)
                                                                     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4128)
                                                                     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4181)
                                                                     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4147)
                                                                     at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4274)
                                                                     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4155)
                                                                     at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4331)
                                                                     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4128)
                                                                     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4181)
                                                                     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4147)
                                                                     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4155)
                                                                     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4128)
                                                                     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4181)
                                                                     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4147)
                                                                     at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4307)
                                                                     at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:4468)
                                                                     at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2435)
                                                                     at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1998)
                                                                     at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1989)
                                                                     at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2412)
                                                                     at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
                                                                     at android.os.MessageQueue.nativePollOnce(Native Method)
                                                                     at android.os.MessageQueue.next(MessageQueue.java:325)
                                                                     at android.os.Looper.loop(Looper.java:142)
                                                                     at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                     at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
11-30 13:49:41.295 1474-1700/? E/memtrack: Couldn't load memtrack module

1 个答案:

答案 0 :(得分:4)

点击对话框按钮后,您应该致电super.onBackPressed()

@Override
public void onBackPressed() {

    HabitEventController hec = new HabitEventController(this);

    if(m != null && m.getPosition() != null){
        hec.setHabitEventLocation(heID, m.getPosition());
   }

   if(m == null || m.getPosition() == null){
       new AlertDialog.Builder(this)
               .setTitle("Really Exit?")
               .setMessage("Are you sure you want to exit, without creating a marker?")
               .setNegativeButton(android.R.string.no, null)
               .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int whichButton) {
                       dialog.dismiss();
                       MapsActivity.super.onBackPressed();
                   }
               }).show();
   }

//Remove this call because your app will close and crash before display the dialog
   // finish();
}