A partire dal 15/2/2012 non ho ancora trovato una buona spiegazione né un motivo per cui ciò non funzioni. La soluzione più vicina a una soluzione è utilizzare l'approccio tradizionale Thread, ma perché includere una classe che non funziona (sembra) nell'SDK di Android?Android SDK AsyncTask doInBackground non in esecuzione (sottoclasse)
Evenin 'SO!
Ho un sottoclasse AsyncTask:
// ParseListener had a callback which was called when an item was parsed in a
// RSS-xml, but as stated further down it is not used at all right now.
private class xmlAsync extends AsyncTask<String, RSSItem, Void> implements ParseListener
che viene eseguito in questo modo:
xmlAsync xmlThread = new xmlAsync();
xmlThread.execute("http://www.nothing.com");
Ora, questa sottoclasse ha incontrato un piccolo errore. In precedenza ha fatto un po 'di xml-analisi, ma quando ho notato che è doInBackground() non è stato chiamato ho messo a nudo il basso, riga per riga, infine, termina con solo questo:
@Override
protected Void doInBackground(String... params)
{
Log.v(TAG, "doInBackground");
return null;
}
Il che, per un po' motivo, non ha registrato nulla. Tuttavia, ho aggiunto questo:
@Override
protected void onPreExecute()
{
Log.v(TAG, "onPreExecute");
super.onPreExecute();
}
E quella riga è effettivamente registrata quando si esegue il thread. Quindi in qualche modo onPreExecute() viene chiamato ma non doInBackground(). Ho un altro AsyncTask in esecuzione in background, allo stesso tempo, che funziona bene.
Attualmente sto eseguendo l'app su un emulatore, SDK versione 15, Eclipse, Mac OS X 10.7.2, vicino al Polo Nord.
EDIT:
@Override
protected void onProgressUpdate(RSSItem... values) {
if(values[0] == null)
{
// activity function which merely creates a dialog
showInputError();
}
else
{
Log.v(TAG, "adding "+values[0].toString());
_tableManager.addRSSItem(values[0]);
}
super.onProgressUpdate(values);
}
_tableManager.addRSSItem() più o meno aggiunge una riga a una SQLiteDatabase, inizializzato con contesto dell'attività. publishProgress() viene chiamato dal callback dell'interfaccia ParseListener. Tuttavia, dal momento che non faccio nient'altro che log.v in doInBackground(), ho scoperto che non era necessario nemmeno farlo apparire.
EDIT 2:
Va bene, solo per essere perfettamente chiaro, questo è l'altro AsyncTask, eseguendo la stessa attività e perfettamente funzionante soddisfacente.
private class dbAsync extends AsyncTask<Void, RSSItem, Void>
{
Integer prevCount;
boolean run;
@Override
protected void onPreExecute() {
run = true;
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
run = true;
prevCount = 0;
while(run)
{
ArrayList<RSSItem> items = _tableManager.getAllItems();
if(items != null)
{
if(items.size() > prevCount)
{
Log.v("db Thread", "Found new item(s)!");
prevCount = items.size();
RSSItem[] itemsArray = new RSSItem[items.size()];
publishProgress(items.toArray(itemsArray));
}
}
SystemClock.sleep(5000);
}
return null;
}
@Override
protected void onProgressUpdate(RSSItem... values) {
ArrayList<RSSItem> list = new ArrayList<RSSItem>();
for(int i = 0; i < values.length; i++)
{
list.add(i, values[i]);
}
setItemsAndUpdateList(list);
super.onProgressUpdate(values);
}
@Override
protected void onCancelled() {
run = false;
super.onCancelled();
}
}
EDIT 3:
Sigh, scusate sto male a fare domande. Ma ecco l'inizializzazione dei compiti.
xmlAsync _xmlParseThread;
dbAsync _dbLookup;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
_dbLookup = new dbAsync();
_dbLookup.execute();
_xmlParseThread = new xmlAsync();
_xmlParseThread.execute("http://www.nothing.com", null);
}
è possibile che l'attività termini prima che l'attività venga completata? –
Altamente improbabile. In tal caso il mio altro thread non funzionerebbe correttamente? E ho solo un'attività in questo momento. – SeruK
La mia app ha lo stesso problema: doInBackground non viene chiamato o chiamato con un ritardo molto lungo. Ecco la mia osservazione limitata: esattamente lo stesso codice funziona perfettamente su uno smartphone Android 2.3.3 e un emulatore Android 2.3.3, ma presenta questo problema su un tablet Android 4.0.3 e un mucchio di emulatori Android 4.x.x. È molto allettante concludere che questo problema è stato introdotto nelle versioni più recenti di Android. – Hong