2015-05-11 161 views
0

我试图在一个侦听器上启动一个模式,我有2个奇怪的运行。对话框不显示android

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

你为什么要调用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(); 
     } 
    }); 
}