为什么我的AlertDialog在onResume()或onActivityResult()中创建时不显示?

时间:2012-01-09 01:12:40

标签: android

我的代码如下所示,从onCreate()调用后效果非常好 我既没有错误也没有stracktrace。它只是没有显示。

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
   .setCancelable(false)
   .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
            finish();
       }
   })
   .setNegativeButton("No", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
       }
   });
AlertDialog alert = builder.create();   
alert.show();

1 个答案:

答案 0 :(得分:2)

尝试..

    AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(this);
    dlgAlert.setMessage("This is an alert with no consequence");
    dlgAlert.setTitle("App Title");
    dlgAlert.setPositiveButton("OK", null);
    dlgAlert.setCancelable(true);
    dlgAlert.create().show();

在代码中的构建器上调用.show()。

相关问题