gpt4 book ai didi

android - 后退按钮崩溃

转载 作者:行者123 更新时间:2023-11-29 15:37:04 24 4
gpt4 key购买 nike

当我点击后退按钮时,我的应用程序崩溃了。我希望当用户在没有创建标记的情况下单击后退按钮时,会弹出一个对话框,提示用户做出决定。但是,当这种情况发生时,标记 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

最佳答案

您应该在单击对话框按钮后调用 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();
}

关于android - 后退按钮崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47583077/

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