AsyncTask 中的 ProgressDialog 未更新或关闭
我使用 AsyncTask 来处理复杂的后台操作(编译要发送的日志文件),并使用 ProgressDialog 来显示用户进度。我尝试过使用 showDialog() 但它似乎从未显示或关闭(它从未被调用),并且我遵循了有关如何执行此操作的教程... 所以我使用的是非托管的,它不会忽略我的消息。我还想在开始编译日志文件时更新消息(因为它似乎滞后 - 或者文本视图可能真的很长,所以它没有像预期的那样更新)。
我已经稍微移动了我的代码,因此看起来存在问题(例如 onProgressUpdate()),但我不知道如何使其工作。我环顾过这个网站,似乎没有什么问题是我遇到的问题(无论如何也不完全是)。 RunOnUiThread() 不起作用, new Thread(){} 不起作用, onProgressUpdate() 我无法开始工作(文档对此感到困惑)。
它也从不驳回。我已经设置了一个监听器,它永远不会消失。
有谁知道我的代码有什么问题吗?我认为 AsyncTask 应该很简单。
private class BuildLogTask extends AsyncTask<Void, Void, String> {
String temp;
ProgressDialog progressdialog = new ProgressDialog(context); //variable is defined at onCreate (held as private, not static)
@Override
protected String doInBackground(Void... params) {
temp = buildLog();
logdata = temp;
publishProgress();
createLogFile();
return temp;
}
protected void onProgressUpdate() {
progressdialog.setMessage("Compiling Log File...");
}
@Override
protected void onPreExecute() {
Log.w(TAG,"Showing Dialog");
send.setEnabled(false);
ProgressDialog progressdialog = new ProgressDialog(context);
progressdialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressdialog.setMessage("Gathering Data...");
progressdialog.setCancelable(false);
progressdialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
Log.e(TAG,"Progress Dialog dismissal.");
}
});
progressdialog.show();
}
@Override
protected void onCancelled(){
Log.e(TAG,"Progress Dialog was Cancelled");
progressdialog.dismiss();
logdata=null;
}
@Override
protected void onPostExecute(String result) {
progressdialog.dismiss();
send.setEnabled(true);
previewAndSend();
}
}
I am using an AsyncTask to handle complex background operations (compiling a log file to send) and I use a ProgressDialog to show the user progress. I have tried using showDialog() but it never seems to show or dismiss (it is never called), and I followed tutorials on how to do it...
So I am using unmanaged ones, and it won't dismiss my message. I am also wanting to update the message as it starts to compile the log file (as it seems to lag there - or maybe the text view is just really long so it doesn't update like it is supposed to).
I have moved my code around a bit so it look like there are problems (like onProgressUpdate()), but I don't know how to make it work. I have looked around this site and nothing seems to be having the problem I am (not exactly anyways). RunOnUiThread() doesn't work, new Thread(){} doesn't work, and onProgressUpdate() I can't get to work (the documentation is confusing on this).
It also never dismisses. I have set up a listener and it never dismisses.
Does anyone know what is wrong with my code? I thought AsyncTask was supposed to be simple.
private class BuildLogTask extends AsyncTask<Void, Void, String> {
String temp;
ProgressDialog progressdialog = new ProgressDialog(context); //variable is defined at onCreate (held as private, not static)
@Override
protected String doInBackground(Void... params) {
temp = buildLog();
logdata = temp;
publishProgress();
createLogFile();
return temp;
}
protected void onProgressUpdate() {
progressdialog.setMessage("Compiling Log File...");
}
@Override
protected void onPreExecute() {
Log.w(TAG,"Showing Dialog");
send.setEnabled(false);
ProgressDialog progressdialog = new ProgressDialog(context);
progressdialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressdialog.setMessage("Gathering Data...");
progressdialog.setCancelable(false);
progressdialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
Log.e(TAG,"Progress Dialog dismissal.");
}
});
progressdialog.show();
}
@Override
protected void onCancelled(){
Log.e(TAG,"Progress Dialog was Cancelled");
progressdialog.dismiss();
logdata=null;
}
@Override
protected void onPostExecute(String result) {
progressdialog.dismiss();
send.setEnabled(true);
previewAndSend();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
那里有两个不同的进度对话框,一个是 onPreExecute() 的本地对话框,另一个是全局对话框。你在 onPostExecution() 中忽略的一个是你的全局的,它从未实际显示过。删除本地声明,它应该可以工作。
You have two different progress dialogs there, one local to onPreExecute() and one global. The one ur dismissing in your onPostExecution() is your global one which was never actually shown. Remove the local declaration and it should work.
有两个问题。
onProgressUpdate 的签名不正确。试试这个:
@覆盖
protected void onProgressUpdate(Void... 进度) {
Progressdialog.setMessage("正在编译日志文件...");
onProgressUpdate
编辑:确定的第二个问题:
There are two problems.
The signature for on onProgressUpdate is not correct. Try this instead:
@Override
protected void onProgressUpdate(Void... progress) {
progressdialog.setMessage("Compiling Log File...");
}
You're masking the progressDialog member variable with a local variable in onPreExecute()
EDIT: Second problem identified:
尝试一下,
替换:
为:
祝你好运。
Try it,
Replace:
For:
Good luck.