2012-07-22 158 views
0

我从Activity的onPostResume方法创建并显示警报对话框。 对话框没有显示,但我不明白为什么。未显示Android警报对话框

我对显示的对话框代码:

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
builder.setMessage("message"); 
builder.setPositiveButton("a", aListener); 
builder.setPositiveButton("b", bListener); 
builder.setCancelable(false); 

AlertDialog dlg = builder.create(); 
dlg.show(); 
+1

包括你的代码,否则它是不可能知道什么是错的 – mdelolmo 2012-07-22 13:27:13

+0

@mdelolmo我同意! :D – 2012-07-22 13:28:20

+1

该代码似乎没问题...你的代码是否正在运行?尝试添加日志消息。另外,你的'onPostResume()'方法签名是什么样的? – dmon 2012-07-22 14:01:52

回答

1

尝试使用:

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
builder.setMessage("message"); 
builder.setPositiveButton("a", aListener); 
builder.setPositiveButton("b", bListener); 
builder.setCancelable(false); 
builder.show(); 

注:没有理由创建另一个AlertDialog实例。


或者你可以创建方法,另一种正确的做法,返回新AlertDialog

protected static final int CREATE_INFORMATION_DIALOG = 1320; 

private Dialog createDialog(int type) { 
     AlertDialog dialog = null; 
     switch (type) { 
      case CREATE_INFORMATION_DIALOG: 
       dialog = new AlertDialog.Builder(this) 
        .setTitle("Information") 
        .setMessage("Download was finished successfully.") 
        .setPositiveButton("Close", new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dlg, int whichButton) { 

         } 
        }) 
        .create(); 
       break; 
     } 
     return dialog; 
    } 

然后就是这样称呼它

createDialog(CREATE_INFORMATION_DIALOG).show(); 
+0

仍然没有显示对话框 – 2012-07-22 13:47:25

+0

尝试使用onResume方法代替onPostResume – Sajmon 2012-07-22 14:18:52

+0

stil不起作用 – 2012-07-22 14:55:36