Dialog不显示android

时间:2015-05-11 15:40:58

标签: java android android-alertdialog

我正在尝试使用一个监听器来启动一个模态,我有两个奇怪的运行。

1)没有任何解释的错误。

2)没有,没有错误,模态不会出现。

这是我的代码:

private void onJoined(JSONObject camp){
    Looper.prepare();

    final Dialog dialog = new Dialog(this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    Window window = dialog.getWindow();
    lp.copyFrom(window.getAttributes());
    //This makes the dialog take up the full width
    lp.width = WindowManager.LayoutParams.MATCH_PARENT;
    window.setAttributes(lp);
    dialog.setContentView(R.layout.modal_layout);

    Button dialogButton = (Button) dialog.findViewById(R.id.modal_btn1);
    // if button is clicked, close the custom dialog
    dialogButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });
    dialog.show();
}

有人可以为此提供帮助吗?

1 个答案:

答案 0 :(得分:1)

你为什么打电话给Looper.prepare?

如果您不在主线程上

,请尝试此操作
private void onJoined(JSONObject camp){
    this.runOnUiThread(new Runnable() {
        public void run() {
            final Dialog dialog = new Dialog(this);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
            WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
            Window window = dialog.getWindow();
            lp.copyFrom(window.getAttributes());
            //This makes the dialog take up the full width
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            window.setAttributes(lp);
            dialog.setContentView(R.layout.modal_layout);

            Button dialogButton = (Button) dialog.findViewById(R.id.modal_btn1);
            // if button is clicked, close the custom dialog
            dialogButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            dialog.show();
        }
    });
}