Android 后退按钮崩溃
当我按下后退按钮时,我的应用程序崩溃了。我希望当用户单击后退按钮而不创建标记时,会弹出一个对话框,提示用户做出决定。然而,当这种情况发生时,标记m返回null,我对此进行了解释,这意味着应用程序上没有标记,但它仍然崩溃Android 后退按钮崩溃,android,google-maps,android-studio,Android,Google Maps,Android Studio,当我按下后退按钮时,我的应用程序崩溃了。我希望当用户单击后退按钮而不创建标记时,会弹出一个对话框,提示用户做出决定。然而,当这种情况发生时,标记m返回null,我对此进行了解释,这意味着应用程序上没有标记,但它仍然崩溃 @Override public void onBackPressed() { super.onBackPressed(); HabitEventController hec = new HabitEventController(this); if(m.
@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();
}
单击对话框按钮后,应调用
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();
}
单击对话框按钮后,应调用
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();
}
你的日志写了什么?检查下面的我的答案你的日志写了什么?检查下面的我的答案在这里检查你的答案逻辑
MapsActivity.super.onBackPressed()代码>对话框。解除()代码>super.onBackPressed()
因此super.onBackPressed被调用了两次,并且还记得在一个内部类中,这就是为什么提问者放置MapsActivity.super.onBackPressed()
而不仅仅是onBackPressed
。谢谢,但它告诉我我无法解析方法super.onBackPressed()@diegoveloperI更新了我的答案:mapsacActivity.super.onBackPressed();由于某种原因,我的应用程序仍然崩溃。当地图上没有点击任何标记时,标记m为null,这应该是可以的,因为我检查了,但随后它崩溃了哦,好的,你没有检查,我将更新我的回答在这里检查你的回答逻辑MapsActivity.super.onBackPressed()代码>对话框。解除()代码>super.onBackPressed()
因此super.onBackPressed被调用了两次,并且还记得在一个内部类中,这就是为什么提问者放置MapsActivity.super.onBackPressed()
而不仅仅是onBackPressed
。谢谢,但它告诉我我无法解析方法super.onBackPressed()@diegoveloperI更新了我的答案:mapsacActivity.super.onBackPressed();由于某种原因,我的应用程序仍然崩溃。当地图上没有点击任何标记时,标记m为空,这应该是可以的,因为我检查了它,但是它崩溃了哦,好的,你没有检查,我会更新我的答案