不显示警报对话框

not showing alertdialog box

    DialogInterface.OnClickListener clickListener= new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which)
                {
                    case BUTTON_POSITIVE :
                        udb.signout();
                        break;

                    case BUTTON_NEGATIVE:
                        finish();
                        break;
                }
            }
        };

        AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
        builder.setTitle("Notification");
        builder.setMessage("You are already logged in.\nDo you want to signout and login with different account?");
        builder.setPositiveButton("Yes",clickListener);
        builder.setNegativeButton("No",clickListener);
        builder.show();

这是我显示弹出对话框的代码..但我遇到了问题 "builder.show()" 行。而且我不明白我做错了什么。请。我会很感激任何帮助

首先调用 builder.create() 。您不能显示构建器本身。

您必须 create 您的 AlertDialog.Builder 对话框然后显示它...删除 builder.show();

AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
builder.setTitle("Notification");
builder.setMessage("You are already logged in.\nDo you want to signout and login with different account?");
builder.setPositiveButton("Yes",clickListener);
builder.setNegativeButton("No",clickListener);   


AlertDialog alert = builder.create();
alert.show();

您需要使用 AlertDialog.Builder 对象创建 AlertDialog 对象并显示对话框。例如,

AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());

/*All your dialog code*/

//Create AlertDialog object
AlertDialog alertDialog = builder.create ();
//show the AlertDialog using show() method
alertDialog.show();

首先,当我在我的设备上亲自测试时,

builder.show();

应该和

效果一样
AlertDialog dialog = builder.create();
dialog.show();

不管是android.support.v7.app.AlertDialog还是android.app.AlertDialog

就我而言,我发现导致问题的原因是 AlertDialog.Builder 的初始化方式。

AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());

Dialog documentation 开始,您需要将 Activity 传递给此构造函数,以下内容将起作用:

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

或者,

AlertDialog.Builder builder = new AlertDialog.Builder(<YourActivity>.this);