【问题标题】:Alert Dialog disappers警报对话框消失
【发布时间】:2017-12-01 13:36:18
【问题描述】:

单击后退按钮时,警报对话框消失。不给我选择的机会。假设当 m == null || 时会弹出此对话框m.getPosition() == null。 “m”是变量“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();
}

【问题讨论】:

  • 什么是 m??????
  • 我的错。 “标记 m”
  • 你知道m 有什么价值吗??并且该检查是有道理的,您的m 不等于null,因此它跳过了其中的代码。
  • 代码有效。我的问题是对话框消失得很快。
  • 尝试setcancelable(false) 获取警报对话框

标签: android android-studio-3.0


【解决方案1】:

你必须用调试模式检查这个地方

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

这里只有问题。

【讨论】:

    【解决方案2】:

    首先你正在检查错误的条件见

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

    1。如果 m 为 null,则第二个条件将抛出 NullPointerException,因为您正在对 null 对象调用 getPosition()

    1. 您在 If 条件中使用 ||(Or) 和 (m==null) 检查,这是完全错误的。

    首先,您将 If 语句设置为正确,然后以下代码将适用于您的场景。

     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
        }
    

    【讨论】:

    • 对不起,我没有看到任何其他方式,你去看看地图上是否没有标记。
    • 检查我的更新答案,这是我能建议的最少。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-11
    • 1970-01-01
    • 2016-10-25
    • 2019-05-15
    • 1970-01-01
    • 2015-08-27
    相关资源
    最近更新 更多