I have a MapActivity that has code similar to this in onCreate():
if(...) {
...
new FirstAsyncTask(id, this).execute((Object[]) null);
...
} else {
...
myLocationOverlay.runOnFirstFix(new Runnable() {
new SecondAsyncTask(lat, lon, ActivityName.this).execute((Object[]) null);
}
...
}
The FirstAsyncTask and SecondAsyncTask both do different things, but both of them show a ProgressDialog like so:
public FirstAsyncTask(long id, Context context) {
progressDialog = new ProgressDialog(context);
}
protected void onPreExecute() {
...
progressDialog.show();
}
protected String doInBackground(Object... params) {
...
progressDialog.dismiss();
...
}
This is working with FirstAsyncTask, but no matter what I change in the call to SecondAsyncTask it always fails with this error: Can’t create handler inside thread that has not called Looper.prepare(). I have tried setting the context parameter to “this”, “ActivityName.this”, getApplicationContext(), and getBaseContext().
I’m still pretty new to Android so this idea of a “context” is confusing me. I’m even more confused that FirstAsyncTask works but SecondAsyncTask doesn’t. I’ve seen this error mentioned a lot in other questions but none of the answers seem to work. Any ideas?
EDIT: The exception is being thrown when the ProgressDialog is being initialized in the SecondAsyncTask’s constructor.
The problem is here
SecondAsyncTask is being constructed on a newly spawned thread. not the UI thread.
Create the one progressDialog in the activity. And then access the Activity’s progressDialog from the Asynctasks.
Do not attempt to perform progressDialog.dismiss() from the doInBackground, instead put it in postExecute which is running in the UI thread.
You’ll want to set up flags once all this is working so that you only dismiss the progressdialog once both tasks are complete.