My problem is related to Android. I have the following code (non-relevant parts omitted):
public class MyActivity extends ListActivity {
protected void onResume() {
super.onResume();
new UpdateTask().execute();
}
private class UpdateTask extends AsyncTask<Object, Object, ListAdapter> {
protected ListAdapter doInBackground(Object... params) {
SQLiteCursor cursor = db.getSomeData();
startManagingCursor(cursor);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, ..., cursor, ..., ...);
return adapter;
}
protected void onPostExecute(ListAdapter result) {
listView.setAdapter(result);
}
}
}
This causes a RuntimeException – “Can’t create handler inside thread that has not called Looper.prepare()” at the line where SimpleCursorAdapter is instantiated.
I understand why this is happening, I just don’t know how to fix it (keeping this in a separate thread). I have found this thread:
http://groups.google.com/group/android-developers/browse_thread/thread/34d0069bb2de925e?fwc=2
But I don’t really understand the reply. I have failed to google any
example involving SQLiteCursor and AsyncQueryHandler.
Could you guys please help? Thanks.
It looks like instantiating the
SimpleCursorAdapterhas to happen on the UI thread. You could do that by havingdoInBackground()returncursorand setting up the adapter inonPostExecute().See Painless Threading if you haven’t already.