没有显示alertdialog框

时间:2016-04-20 17:48:48

标签: android alertdialog

    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()”行。我无法理解我做错了什么。请。我会感激任何帮助

4 个答案:

答案 0 :(得分:2)

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

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开始,您需要将活动传递给此构造函数,以下内容将起作用:

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

,或者

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

答案 1 :(得分:1)

您必须//the directive .directive('dynamicTag', ['$compile', function($compile){ return{ restrict: 'A', scope: { tag: '=' }, link: function(scope, element, attrs){ var getTemplate = function() { var template = ''; if (scope.tag.type === 'text') { template = '{{tag.title}}' + '<br />' + '<input type="{{tag.type}}" ' + 'id="{{tag.id}}" ' + 'title="{{tag.summaryTitle}}" ' + 'name="{{tag.paramName}}" ' + 'placeholder="{{tag.placeholder}}" ' + 'required="{{tag.required}}" ' + '/><br />'; } return template; }; var compile = function() { var template = getTemplate(); element.append(template); $compile(element.contents())(scope); }; compile(); } } }]); //in the html code: <div ng-repeat="tag in content" dynamic-tag tag="tag"></div> create对话,然后显示...删除AlertDialog.Builder

builder.show();

答案 2 :(得分:0)

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

答案 3 :(得分:0)

您需要使用AlertDialog.Builder对象和show对话框创建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();