Android:TabActivity - 在 AsyncTask 完成之前不添加选项卡
我有一个实现 TabContentFactory 的 TabActivity,它在 onCreate() 中启动一个 AsyncTask 来获取选项卡的数据。当 AsyncTask 完成时,onPostExecute() 可以直接更新 UI 元素,对吗?这意味着,由于该方法在 UI 线程中运行,因此在访问 UI 元素时不需要进一步的线程同步?
不管怎样,我遇到的问题是我的 UI 线程在 TabActivity 中调用 createTabContent(),而 AsyncTask 仍然很忙。我必须添加至少一个选项卡,否则我会收到 NullPointerException。但是,如何仅在 AsyncTask 完成且 ProgressDialog 已关闭时添加选项卡?
如果有人能提供帮助,我会很高兴...
I've got an TabActivity implementing TabContentFactory, which starts an AsyncTask in onCreate() to fetch the data for the tabs. When the AsyncTask has finished, onPostExecute() could directly update the UI-elements, right? Meaning, since that method runs in the UI-Thread no further thread-synchronization would be required when accessing UI-elements?
Anyway, the problem I have is that my UI-Thread calls createTabContent() in the TabActivity while the AsyncTask is still busy. I have to add at least one tab, or I get a NullPointerException. But how do I only add tabs when my AsyncTask has finished and the ProgressDialog has been dismissed?
I'd be glad if someone could help...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正确的。
如果您需要在 AsyncTask 仍在后台运行时更新 UI,请重写 AsyncTask.onProgressUpdate(..),然后从 AsyncTask.doInBackground(..) 中调用 AsyncTask.publishProgress(..)。
我不明白这一点。您能更详细地解释一下吗?
无论如何,请注意以下事项:
Right.
If you need to update UI while AsyncTask is still running in background, then override AsyncTask.onProgressUpdate(..) and then call AsyncTask.publishProgress(..) from within the AsyncTask.doInBackground(..).
I don't understand this. Could you please explain in more detail?
Anyway, watch for this things: