简体   繁体   English

没有显示alertdialog框

[英]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();

this my code for showing pop up dialog box.. but I'm getting problem on "builder.show()" line. 这是我显示弹出对话框的代码..但我在“builder.show()”行上遇到问题。 And I can't understand what i did wrong. 我无法理解我做错了什么。 Please. 请。 I'll appreciate any help 我会感激任何帮助

First of all, as I tested personally on my device, 首先,当我在我的设备上亲自测试时,

builder.show();

should has the same effect as 应该具有相同的效果

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

no matter android.support.v7.app.AlertDialog or android.app.AlertDialog is used. 无论使用android.support.v7.app.AlertDialog还是android.app.AlertDialog

In my case, I found what is causing the problem is the way AlertDialog.Builder is initialized. 在我的例子中,我发现导致问题的原因是AlertDialog.Builder的初始化方式。

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

From Dialog documentation , you need to pass in an Activity to this constructor, which the followings will work: Dialog文档中 ,您需要将Activity传递给此构造函数,以下内容将起作用:

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

or, 要么,

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

You have to create dialog of your AlertDialog.Builder and then show it...remove builder.show(); 您必须create AlertDialog.Builder对话框,然后显示它... remove 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();

First call builder.create() . 首先调用builder.create()。 You cannot display the builder itself. 您无法显示构建器本身。

You need to create AlertDialog object using the AlertDialog.Builder object and show dialog. 您需要使用AlertDialog.Builder对象和show对话框创建AlertDialog对象。 For eg, 例如,

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

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

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