2012-07-22 182 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