【问题标题】:Android - AlertDialog doesn't show when called from Main-MethodAndroid - 从 Main-Method 调用时不显示 AlertDialog
【发布时间】:2013-01-04 19:52:14
【问题描述】:

我的 AlertDialog 有问题。当我把它放在按钮的 onClick-Listener 中时,代码运行良好,但是当我把它放在我的主方法末尾时它根本不起作用。

这是显示 AlertDialog 的方法:

void showMaths(){
    AlertDialog.Builder alert = new AlertDialog.Builder(LabyRiddle.this);

    alert.setTitle("Title");
    alert.setMessage("Message");

    // Set an EditText view to get user input 
    final EditText input = new EditText(LabyRiddle.this);
    alert.setView(input);

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
      }
    });

    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int whichButton) {
      }
    });

    alert.create();  // <== Doesn't make any difference whether its here or not
    alert.show();
}

我只希望 main-method 运行并在它结束时出现警报...

但它不会出现,也不会崩溃甚至报错。

或者是否有另一种可能在 main 方法完成后显示警报?

谢谢,祝你有美好的一天!

奥利弗

【问题讨论】:

  • 作者,问题解决了吗?
  • 不,我没有找到答案... :/ 但它不再那么重要了 ;)

标签: android show android-alertdialog main


【解决方案1】:

你忘了打这个电话

 alert.create();

之前

alert.show();

有关警报对话框的更多信息,请参阅此链接 http://developer.android.com/guide/topics/ui/dialogs.html#AlertDialog

【讨论】:

    【解决方案2】:

    我觉得你需要打电话

    alert.create().show();
    

    在调用show(); 之前,您必须使用create(); 方法从构建器创建对话框

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多