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?
我不明白这一点。你能更详细地解释一下吗?
无论如何,请注意这些事情:
- 只有在 TabActivity 完全创建后才启动 AsyncTask。从 onPostCreate() 而不是 onCreate() 开始。这可能是 NullPointerException 的来源。
- 您可以从 AsyncTask.onPostExecute(..) 中的 UI 线程更新任何活动
- 如果您需要在 AsyncTask 仍在后台运行时更新 UI,请从 AsyncTask.doInBackground(..) 中调用 AsyncTask.publishProgress(..)