简体   繁体   中英

Dialog doesn't show android

I'm trying on a listener to launch a modal, and I have 2 weird runs.

1) Error without any explanation.

2) Nothing, no error, the modal just doesn't appear.

Here is my code :

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

Can someone help on this ?

Why are you calling Looper.prepare ?

Try this if you're not on main thread :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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