Android AlertDialog inside AsyncTask

I have a list that has checkboxes. For each flag (they are about 3) there is a specific AsyncTask for it.

I never know which users choose, so I can’t put Async AlertDialog at the end of the task, because I never know if the user has selected only one flag, or two or three.

Since AsyncTask is executed in steps (only when the first Async is complete, when the second starts), I thought about putting an end to the whole new AsyncTask using 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 a mistake:

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 as follows:

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 in this line is:

alertDialog = new AlertDialog.Builder(getApplicationContext()).create();
+4
source share
4 answers
Dialog window

alert - , async-.

private class showMessageAsync extends AsyncTask<String, Void, String> {
     AlertDialog alertDialog;
     protected void onPreExecute() {
    super.onPreExecute();
            alertDialog = new AlertDialog.Builder(YourClasss.this);  
     }
     @Override
     protected String doInBackground(Void... params){       
            return null;
     }
     @Override
     protected void onPostExecute(String result) {
            super.onPostExecute(result);

            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();
    }

}
+21

: java.lang.RuntimeException: , Looper.prepare()

lertdialog doInbackground. doInbackground backgroudn. ui ui.

doInbackground ui onPostExecute. runOnUiThread, . onProgressUpdate(Progress...)

http://developer.android.com/reference/android/os/AsyncTask.html

Threads @http://developer.android.com/guide/components/processes-and-threads.html

, , "main". , , . , Android UI ( android.widget android.view). , .

  alertDialog = new AlertDialog.Builder(ActivityContext).create();
+3

you cannot call a warning window inside the do inbackground method.same for the toast. No user interface operations can be performed inside the doinbg method. use the post post method instead, or change the way everything should be done.

+2
source

you can access ui with this method inside the doInBackground ... function:

getActivity().runOnUiThread(new Runnable() {
    @Override
    public void run() {
        if (!dialog.isShowing()) {
            dialog.show();
        }
        dialog.setMessage(getText(R.string.lbl_downloading) + " : " + String.valueOf(mCount) + " / " + String.valueOf(totalItems));
    }
});

this is a snapshot of my code that displays items with reduced load.

0
source

All Articles