Reputation: 6187
I'm using an ORM for Android called Sugar to persist my models on the database and I'm using it inside my AsyncTask.
Here is its declaration:
public class LoginTask extends AsyncTask<Object, Integer, String> {
private Context context;
private ProgressDialog progressDialog;
public LoginTask(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(context) {
{
setMessage("Authenticating...");
setTitle("Login");
setCancelable(false);
setIndeterminate(true);
show();
}
};
}
@Override
protected String doInBackground(Object... params) {
String email = (String) params[0];
String password = (String) params[1];
try {
User user = LoginWebService.loginUser(email, password,
context);
user.save();
} catch (CommunicationException e) {
e.printStackTrace();
return e.getMessage();
}
return null;
}
@Override
protected void onPostExecute(final String result) {
progressDialog.dismiss();
}
}
The line user.save() above, that saves the user model in the db, is the one that causes the exception. The strange thing is that if I declare the task above as an inner class from the activity, it works fine, but if I declare the task on a separate file, it throws this exception:
E/AndroidRuntime(17172): at com.app.task.LoginTask.doInBackground(LoginTask.java:47)
E/AndroidRuntime(17172): at com.app.task.LoginTask.doInBackground(LoginTask.java:1)
E/AndroidRuntime(17172): at android.os.AsyncTask$2.call(AsyncTask.java:264)
E/AndroidRuntime(17172): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
E/AndroidRuntime(17172): ... 5 more
E/AndroidRuntime(17172): Caused by: java.lang.RuntimeException: Cant create handler inside thread that has not called Looper.prepare()
I can't see what makes the difference as I can't see any sense on this.
Upvotes: 3
Views: 1064
Reputation: 443
Are you using version 1.3?
I'm using version 1.3 not using DOMAIN_PACKAGE_NAME in my manifest and do the same thing inside an AsyncTask, actually i'm doing a lot of database work inside AsyncTask with a ProgressDialog...
If you want to stress out some things, my application tag in my manifest has the android:persistent="true" tag add.
Also my main activity has the android:launchMode="singleTask", but im also calling some AsyncTask from others Activity's.
Upvotes: 0
Reputation: 393
I see that this is a very old thread but I would like to put in some efforts to answer this for those who are referring to this in future.
I had a similar problem and the below steps fixed the issue.
You can avoid or workaround this by appropriately setting,
<meta-data android:name="DOMAIN_PACKAGE_NAME" android:value="com.example" />
in your AndroidManifest.xml
It has been mentioned in the original guide that these parameters are all optional but without this parameter set, you may encounter the above exception.
I have also opened an issue on the main repo requesting to update the guide here
Upvotes: 0
Reputation: 1699
You are trying to access/update the UI from a background thread. The exception that is thrown in the last sentence of log cat indicates "these" kind of interactions. This would also crash if it was an inner class of an activity.
The proof is that you are passing the context
. Another question because that might the problem too. This is the context of the activity or the context of the AsyncTask? What context does it require?
Upvotes: 3