警报对话框分配器 [英] Alert Dialog disappers

查看:104
本文介绍了警报对话框分配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

警告对话框"消失.没有给我机会进行选择.假定当m == null ||时弹出此对话框. m.getPosition()== null. "m"是变量"Marker m"

Alert Dialog disappears when the back button is clicked. Does no give me the opportunity to make a selection. This dialog is suppose to pop up when m == null || m.getPosition() == null. "m" is the variable"Marker 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();
}

推荐答案

首先,您正在检查错误的条件,参见

first of all You are checking wrong condition see

if(m == null || m.getPosition() == null)

1.如果m为null,则当您在空对象上调用 getPosition()时,第二个条件将引发 NullPointerException .

1. if m is null then the second condition will throw NullPointerException as you are calling getPosition() on a null object.

  1. 您在if条件中使用||(Or)并进行(m == null)检查,这是完全错误的.

首先,您正确地执行了 If 语句,然后以下代码将在您的方案中起作用.

First you make it right the If statement then the following code will work in your scenario.

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

要检查场景中的标记,最好创建这样的方法:

To check Marker in your scenario its better to create a method like this one :

   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
    }

这篇关于警报对话框分配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆