[英]Dialog doesn't show android
我正在嘗試在偵聽器上啟動模式,並且我有2次奇怪的運行。
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();
}
有人可以幫忙嗎?
為什么要調用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();
}
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.