0

我有一个对话框和一个线程。线程完成后,我想执行对话框。问题有时是执行,有时不是。我不明白为什么会这样。

我该如何解决?

/** display dialog */
private void showDialog(int status){
    final Dialog dialog = new Dialog(getView().getContext());
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
    dialog.getWindow().setFlags(LayoutParams.FLAG_FULLSCREEN, LayoutParams.FLAG_FULLSCREEN);                        
    dialog.setContentView(R.layout.custom_dialog);
    dialog.getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
    dialog.setCancelable(false);
    ImageView ivCustomDialog = (ImageView) dialog.findViewById(R.id.ivCustomDialog);

    Button dialogButton = (Button) dialog.findViewById(R.id.btnProsseguir);
    dialogButton.setOnClickListener(new OnClickListener() {             
        @Override
        public void onClick(View v) {                                           
            dialog.dismiss();                   
        }
    });             

    dialog.show();                      
}

线

new Thread(){
    public void run(){
        int count = 0;
        while(count < 100){
            if(count >= 100){                                                       
                getActivity().runOnUiThread(new Runnable() {                                                        
                    @Override
                    public void run() {                                                             
                        showDialog(1);  
                        interrupt();
                    }
                });                                                         
            }
            count++;
        }
    }                                           
}.start();
4

1 回答 1

1

你最好传递一个有效的上下文......问题存在于正确传递上下文......你最好使用AsyncTask......

http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html

于 2014-12-05T12:59:50.367 回答