Android:TabActivity - 在 AsyncTask 完成之前不添加选项卡

发布于 2024-09-28 21:57:31 字数 377 浏览 1 评论 0原文

我有一个实现 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄居人 2024-10-05 21:57:31
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.

如果您需要在 AsyncTask 仍在后台运行时更新 UI,请重写 AsyncTask.onProgressUpdate(..),然后从 AsyncTask.doInBackground(..) 中调用 AsyncTask.publishProgress(..)。

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?

我不明白这一点。您能更详细地解释一下吗?

无论如何,请注意以下事项:

  1. 仅在 TabActivity 完全创建后才启动 AsyncTask。从 onPostCreate() 而不是 onCreate() 启动。这可能是 NullPointerException 的来源。
  2. 您可以在 AsyncTask.onPostExecute(..) 内更新 UI 线程中的任何活动。
  3. 如果您需要在 AsyncTask 仍在后台运行时更新 UI,请从 AsyncTask.doInBackground(..) 内调用 AsyncTask.publishProgress(..)
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?

Right.

Anyway, the problem I have is that my UI-Thread calls createTabContent() in the
TabActivity while the AsyncTask is still busy.

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 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 don't understand this. Could you please explain in more detail?

Anyway, watch for this things:

  1. Start AsyncTask only after TabActivity is fully created. Start it from onPostCreate() instead of onCreate(). This might be a source of your NullPointerException.
  2. You can update any activities from UI thread within AsyncTask.onPostExecute(..)
  3. If you need to update UI while AsyncTask is still running in background, then call AsyncTask.publishProgress(..) from within the AsyncTask.doInBackground(..)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文