I have a listview which have checkboxes. For each checkbox (they are about 3), it has a specific AsyncTask for it.
I never know what checkboxes user chooses, so I cannot put in the end of the Async task the AlertDialog, because I never know if the user has chosen only one checkbox, or two or three.
Because the AsyncTask are executed in steps (only when the 1st Async is finished is when the second begins), I thought about put in the end of everything a new AsyncTask with an AlertDialog.
private class showMessageAsync extends AsyncTask<Void, Integer, String> {
@Override
protected String doInBackground(Void... params){
AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(getApplicationContext).create();
alertDialog.setTitle("The Process");
alertDialog.setIcon(R.drawable.success);
alertDialog.setCanceledOnTouchOutside(false);
alertDialog.setMessage("All done!");
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent A = new Intent(DownloadActivity.this, Menu_activity.class);
startActivity(A);
finish();
}
});
alertDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
Intent A = new Intent(DownloadActivity.this, Menu_activity.class);
startActivity(A);
finish();
}
});
alertDialog.show();
return "Executed";
}
}
And this is the error:
10-21 04:24:34.117: E/AndroidRuntime(1026): FATAL EXCEPTION: AsyncTask
#4 10-21 04:24:34.117: E/AndroidRuntime(1026): java.lang.RuntimeException: An error occured while executing
doInBackground() 10-21 04:24:34.117: E/AndroidRuntime(1026): at
android.os.AsyncTask$3.done(AsyncTask.java:299) 10-21 04:24:34.117:
E/AndroidRuntime(1026): at
java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
10-21 04:24:34.117: E/AndroidRuntime(1026): at
java.util.concurrent.FutureTask.setException(FutureTask.java:219)
10-21 04:24:34.117: E/AndroidRuntime(1026): at
java.util.concurrent.FutureTask.run(FutureTask.java:239) 10-21
04:24:34.117: E/AndroidRuntime(1026): at
android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 10-21
04:24:34.117: E/AndroidRuntime(1026): at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
10-21 04:24:34.117: E/AndroidRuntime(1026): at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
10-21 04:24:34.117: E/AndroidRuntime(1026): at
java.lang.Thread.run(Thread.java:841) 10-21 04:24:34.117:
E/AndroidRuntime(1026): Caused by: java.lang.RuntimeException: Can't
create handler inside thread that has not called Looper.prepare()
10-21 04:24:34.117: E/AndroidRuntime(1026): at
android.os.Handler.<init>(Handler.java:197) 10-21 04:24:34.117:
E/AndroidRuntime(1026): at
android.os.Handler.<init>(Handler.java:111) 10-21 04:24:34.117:
E/AndroidRuntime(1026): at android.app.Dialog.<init>(Dialog.java:107)
10-21 04:24:34.117: E/AndroidRuntime(1026): at
android.app.AlertDialog.<init>(AlertDialog.java:114) 10-21
04:24:34.117: E/AndroidRuntime(1026): at
android.app.AlertDialog$Builder.create(AlertDialog.java:931) 10-21
04:24:34.117: E/AndroidRuntime(1026): at com.example.MyExample.DownloadActivity$showMessageAsync.doInBackground(DownloadActivity.java:487)
10-21 04:24:34.117: E/AndroidRuntime(1026): at com.example.MyExample.DownloadActivity$showMessageAsync.doInBackground(DownloadActivity.java:1)
10-21 04:24:34.117: E/AndroidRuntime(1026): at android.os.AsyncTask$2.call(AsyncTask.java:287) 10-21 04:24:34.117:
E/AndroidRuntime(1026): at java.util.concurrent.FutureTask.run(FutureTask.java:234) 10-21
04:24:34.117: E/AndroidRuntime(1026): ... 4 more
I call my AsyncTask this way:
if(list.get(0).isSelected() == true){
// list = class that contains checkboxs state
String[] params = {order, String.valueOf(limit_customers) };
customers.execute(params);
}
if(list.get(1).isSelected() == true){
String[] params = {order, String.valueOf(limit_products) };
products.execute(params);
}
// etc, and in the end of this:
showMessageAsync sM = new showMessageAsync();
sM.execute();
The error is on this line:
alertDialog = new AlertDialog.Builder(getApplicationContext()).create();