AsyncTask 中的进度条未更新
我正在尝试在下载一些东西时更新 AsyncTask 中的进度条..
下面是代码:
@Override
protected void onPreExecute() {
Intent myIntent = new Intent(mContext, Explanation.class);
mContext.startActivity(myIntent);
View inflater = View.inflate(mContext, R.layout.explanation, null);
mProgressDialog = (ProgressBar) inflater.findViewById(R.id.Explanation_ProgressBar);
}
@Override
protected void onProgressUpdate(Integer... values) {
mProgressDialog.setProgress(values[0]);
}
xml:
<ProgressBar
android:max="100"
style="?android:attr/progressBarStyleHorizontal"
android:layout_alignParentBottom="true" android:layout_width="fill_parent" android:id="@+id/Explanation_ProgressBar"
android:layout_height="wrap_content"></ProgressBar>
注意:进度条位于我在 onPreExecute() 中调用的 Activity 的布局中。我想通过 AsyncTask 更新它。这可能吗?为什么不更新?
I'm trying to update the progress bar in a AsyncTask while downloading some stuff..
Here is the code:
@Override
protected void onPreExecute() {
Intent myIntent = new Intent(mContext, Explanation.class);
mContext.startActivity(myIntent);
View inflater = View.inflate(mContext, R.layout.explanation, null);
mProgressDialog = (ProgressBar) inflater.findViewById(R.id.Explanation_ProgressBar);
}
@Override
protected void onProgressUpdate(Integer... values) {
mProgressDialog.setProgress(values[0]);
}
xml:
<ProgressBar
android:max="100"
style="?android:attr/progressBarStyleHorizontal"
android:layout_alignParentBottom="true" android:layout_width="fill_parent" android:id="@+id/Explanation_ProgressBar"
android:layout_height="wrap_content"></ProgressBar>
A note: The progress bar is in the layout of the Activity I call in the onPreExecute(). and I want to update it through the AsyncTask. Is this possible? Why isnt it updated?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要更新进度对话框,您需要从
doInBackground
调用publishProgress(...)
。检查 AsyncTask 文档 的“使用情况”和“publishProgress”部分
To update progress dialog you need to call
publishProgress(...)
fromdoInBackground
.Check Usage and publishProgress sections of AsyncTask doc
doInBackground 线程无法访问UI 线程。 onProgressUpdate 允许您将更新发布到 UI 线程。您的代码应如下所示:
The doInBackground thread can not access the UI thread. The onProgressUpdate allows you to post updates to the UI thread. This is what your code should look like: