简体   繁体   English

Android警报对话框未显示

[英]Android alert dialog not shown

I create and show an alert dialog from onPostResume method of the Activity. 我从Activity的onPostResume方法创建并显示一个警报对话框。 The dialog is not shown but i cannot understand why. 该对话框未显示,但我不明白为什么。

My code for showing the dialog: 我显示对话框的代码:

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();

Try to use: 尝试使用:

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

Note: There is no reason to create another AlertDialog instance. 注意:没有理由创建另一个AlertDialog实例。


Or another correct approach you can create method that returns new 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;
    }

And then just call it like 然后像这样称呼它

createDialog(CREATE_INFORMATION_DIALOG).show();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM