有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java对话框不显示安卓

我试着让一个监听器启动一个模态,我有两次奇怪的运行

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(安卓.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) 个答案

  1. # 1 楼答案

    你为什么打给Looper。准备好了吗

    如果你不在主线程上,试试这个:

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