Android警报对话框未显示

时间:2012-07-22 13:25:26

标签: android android-alertdialog

我从Activity的onPostResume方法创建并显示一个警告对话框。 对话框未显示但我无法理解原因。

我显示对话框的代码:

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 个答案:

答案 0 :(得分: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();