因此,在我的对话框中,我有一个按钮启动Asynctask下载器从我的服务器获取某个文件;而且工作得很好。但是,我想关闭当前的对话框并在点击按钮上显示ProgressDialog。我将如何处理这个问题?Android对话框和AsyncTask导致UI冻结
目前,如果我点击按钮(我的对话框中的一个元素),整个UI会在下载器从服务器下载文件时冻结。下载程序完成其任务后,进度对话框将显示。
我的代码看起来是这样的
MainActivity{
Dialog dialog;
ProgressDialog progress;
onCreate(){
dialog = new Dialog(this);
progress = new ProgressDialog(this);
dialog.setContentView(Some View Resource With My Button);
someMethod();
dialog.show();
}
someMethod(){
Button button = (Button) dialog.findViewById(resource of button);
button.setOnClickListener(new OnClickListener(){
onClick(){
dialog.dismiss();
progress.show();
new DownloaderAsyncTask(progress).execute().get();
}
//go to another activity when download finished
});
}
private class DownloaderAsyncTask extends AsyncTask<Void, Void, Void>{
ProgressDialog progress;
DownloaderAsyncTask(ProgressDialog progress){
this.progress = progress;
}
doInBackGround(){
//Downloading
}
onPostExecute(){
//Kill connection
this.progress.dismiss();
}
}
}
感谢。请让我知道,如果你们需要任何额外的信息。
显示您的logcat –
没有错误。用户界面被冻结,因为它正在等待下载器完成。下载器完成后,一切正常。但是,我希望当前的对话框被忽略,并且只要单击该按钮,就会显示progressDialog。 – Infinity
不要调用'get()'。看到答案。 –