当单击"返回"按钮时,警报对话框消失。没有给我机会做出选择的机会。当m == null ||时,该对话框应该弹出m.getPosition()== null。" m"是变量"标记M"
@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();
}
您必须使用调试模式在此地方检查
if(m == null || m.getPosition() == null)
这里只有问题。
首先您要检查错误的条件,请参见
if(m == null || m.getPosition() == null)
1。如果m为null,则第二个条件将在null对象上呼叫 getPosition() getPosition() nullpoInterException 。
- 您正在使用(m == 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)
.setCancelable(false)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
}).show();
在您的方案中查看标记最好创建这样的方法:
private boolean isMarkerAvailable() {
if (m == null)
return false;
else if (m.getPosition() == null)
return false;
return true;
}
if (!isMarkerAvailable()) {
// Show your alert here or you can toggle
// the condition whatever is apropriate in your scenario
}