Android: TabActivity - aggiungere nessun Tabs prima AsyncTask ha finito
-
25-09-2019 - |
Domanda
Ho un TabActivity attuazione TabContentFactory, che inizia un AsyncTask in onCreate () per recuperare i dati per le schede. Quando l'AsyncTask ha terminato, OnPostExecute () potrebbe aggiornare direttamente l'UI-elementi, giusto? Significato, dal momento che il metodo viene eseguito in UI-thread nessun ulteriore filo-sincronizzazione sarebbe necessaria quando si accede a UI-elementi?
In ogni caso, il problema che ho è che il mio UI-thread chiama createTabContent () nella TabActivity mentre l'AsyncTask è ancora occupato. Devo aggiungere almeno una scheda, o ottengo un NullPointerException. Ma come faccio solo aggiungere le schede quando il mio AsyncTask ha finito e il ProgressDialog è stato licenziato?
Sarei felice se qualcuno potesse aiutare ...
Soluzione
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?
A destra.
Anyway, the problem I have is that my UI-Thread calls createTabContent() in the
TabActivity while the AsyncTask is still busy.
Se avete bisogno di aggiornamento dell'interfaccia utente, mentre AsyncTask è ancora in esecuzione in background, quindi sovrascrivere AsyncTask.onProgressUpdate (..) e quindi chiamare AsyncTask.publishProgress (..) dall'interno del 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?
Non capisco questo. Potrebbe spiegare più in dettaglio?
In ogni caso, orologio per queste cose:
- Avvia AsyncTask solo dopo TabActivity è completamente creato. Inizia da onPostCreate () al posto di onCreate (). Questo potrebbe essere una fonte a tua NullPointerException.
- È possibile aggiornare qualsiasi attività dal thread dell'interfaccia utente all'interno AsyncTask.onPostExecute (..)
- Se avete bisogno di aggiornamento dell'interfaccia utente, mentre AsyncTask è ancora in esecuzione in background, quindi chiamare AsyncTask.publishProgress (..) dall'interno del AsyncTask.doInBackground (..)