Difficulty in changing the message of progress dialog in async task
我创建了一个异步任务,并希望在 doBackground 的不同阶段更改进度对话框的消息。这是代码:
|
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
public class sc extends AsyncTask<Integer,String,Void>
{ ProgressDialog dialog; protected void onPreExecute() { dialog=new ProgressDialog(Loc.this); dialog.show(); } @Override protected Void doInBackground(Integer… params) { onProgressUpdate(“Contacting server..Please wait..”); } |
但问题是,进度对话框总是只显示第一条消息。请帮助我找到解决方案。
- 您是否确认了非常基本的调试并确保它实际上完全进入了进度更新方法?
- 这是从您的代码中剪切/粘贴的吗?代码中有一个错字:您的 ‘onProgressUpdate mth 缺少一个 ‘r’
- @JoxTraex:第一条消息是在进度更新的帮助下设置的。问题是在那之后它不会接收下一条消息,而是在进度更新调用之间执行其他代码
|
1
2 3 4 5 |
protected Void doInBackground(Integer… params)
{ onProgessUpdate(“Contacting server..Please wait..”); … } |
Urrrm,不,那行不通。
试试……
|
1
|
publishProgress(“Contacting server..Please wait..”);
|
您必须在 doInBackground(..) 中”发布”您的进度才能调用 onProgressUpdate(…)。
也不要在 doInBackground(…) 中调用 dialog.dismiss() 而是在 onPostExecute(…) 中调用它。
- 这里当我申请 publishProgress(“Contacting server.. Please wait..”);然后它显示错误: AsyncTask<String,Integer,Void> 类型中的方法 publishProgress(Integer…) 不适用于参数 (String)
- @VinitVikash:您需要一个带有 <String,String,Void> 签名的 AsyncTask – 它是第二个参数类型,它指示传递给 onProgressUpdate() 的内容。
我认为应该是..
|
1
|
publishProgress(“Your Dialog message..”);
|
不是
|
1
|
onProgessUpdate(“Processing the result”);
|
在doInBack..()中
类似,
|
1
2 3 4 5 6 7 8 9 |
protected Long doInBackground(URL… urls) {
publishProgress(“Hello”); return null; } protected void onProgressUpdate(String msg) { } |
问题也可能是您没有设置”初始消息”。如果您在尝试在 onProgressUpdate 内部设置消息之前没有为 ProgressDialog 设置消息,它将不起作用。
|
1
2 3 4 5 6 7 |
ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setTitle(“Title”); progressDialog.setMessage(“Initial message needed”); public class foo extends AsyncTask<Void,Integer,Void> { |
还请注意,如果您需要进度更新和消息,则可以使用整数变量参数,其中一个整数定义进度量,另一个定义消息作为 String[] 数组的索引消息数量(如果事先知道消息)。
来源:https://www.codenong.com/9063112/
