Android自定义对话框未显示

时间:2015-10-22 20:06:18

标签: android dialog alertdialog

我想通过显示自定义对话框收到消息后立即通知用户。我有一个自定义的对话框布局,并引用它:

builder = new AlertDialog.Builder(this);
        dia = builder.create();
builder = new AlertDialog.Builder(MapActivity.this); //builder = alertbuilder object
            LayoutInflater inflater = getLayoutInflater();
            View dialoglayout = inflater.inflate(R.layout.dialog_layout, null);
            builder.setView(dialoglayout);
            builder.setTitle("REQUEST");
            builder.setMessage("Would you like to accept it?");
            //builder.show();
            dia.show(); //dia = dialog object

            spRequest.play(spSoundId, 1, 1, 1, 15, 1);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        dia.cancel();

                    }
                }, 7000); 

我想播放一个声音池并显示对话框7分钟然后停止。但是声池播放但不是对话框,而是屏幕暗了7分钟。我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:0)

您好我是初学者,但我认为您在LayoutInflater行中使用了一个Command。 尝试添加getActivity()它需要看起来像这样。

 LayoutInflater inflater = getActivity().getLayoutInflater();

我希望我帮助过你。

答案 1 :(得分:0)

解决:我正在使用空构建器创建对话框。在将对象分配给对话框对象之前,顺序是构建器对象应填充自定义布局和所需的所有内容,否则对话框将显示但它将为空白。以下是像我一样受苦的人的正确代码:)

builder = new AlertDialog.Builder(MapActivity.this);
            LayoutInflater inflater = getLayoutInflater();
            View dialoglayout = inflater.inflate(R.layout.dialog_layout, null);
            builder.setView(dialoglayout);
            builder.setTitle("REQUEST");
            builder.setMessage("Would you like to accept?");
            dia = builder.create();
            //builder.show();
            dia.show();

            spRequest.play(spSoundId, 1, 1, 1, 15, 1);
               new Handler().postDelayed(new Runnable() {
                   @Override
                   public void run() {
                       dia.dismiss();
                   }
               }, 7000);    //the alert will play for 7 seconds and stop