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.
如果您需要更新UI时的AsyncTask仍在后台运行,然后覆盖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?
我不明白这一点。你能否详细解释一下?
总之,看这件事情:
- 开始的AsyncTask之后才完全创建TabActivity。从onPostCreate()开始,而不是onCreate()。这可能是你的NullPointerException的一个来源。
- 可以内AsyncTask.onPostExecute(..)
- 更新从UI线程的任何活动。如果您需要更新UI时的AsyncTask仍在后台运行,然后从内部的AsyncTask调用AsyncTask.publishProgress(..)。 doInBackground(..)
嗨,谢谢你的回答!你说我应该在完全创建TabActivity之后才启动AsyncTask - 这正是我的问题。我不想在AsyncTask完成之前显示任何选项卡。 : -/ – cody 2010-10-23 18:39:29
我忘记解释了:一切正常,我只想让UI线程等待数据。我考虑用包含一个空ScrollView的5个选项卡初始化TabActivity,然后在AsyncTask.onPostExecute(..)中向这些选项添加内容。但我不确定这是否会奏效......? – cody 2010-10-23 18:47:57
它应该工作。小部件初始化后,您可以随时添加/更改内容。为了方便用户,您应该显示ProgressBar并在加载后将其替换为内容。随着内容可用,您可以逐步更新(选项卡之后的选项卡)。见上面第3点。 – 2010-10-23 19:01:58