我有一个实现TabContentFactory的TabActivity,它在onCreate()中启动AsyncTask来获取选项卡的数据。当AsyncTask完成后,onPostExecute()可以直接更新UI元素,对吧?这意味着,因为该方法在UI-Thread中运行,所以在访问UI元素时不需要进一步的线程同步吗?
无论如何,我遇到的问题是我的UI-Thread在TabActivity中调用createTabContent()而AsyncTask仍然很忙。我必须添加至少一个选项卡,否则我会得到一个NullPointerException。但是,当我的AsyncTask完成并且ProgressDialog被解除时,我如何才添加标签?
如果有人可以提供帮助,我会很高兴......
答案 0 :(得分:1)
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?
我不明白这一点。你能详细解释一下吗?
无论如何,请注意这些事情: