Android的AlertDialog里面的AsyncTask里面、Android、AlertDialog、AsyncTask

2023-09-05 10:36:31 作者:秋末♂残雪℡

我有一个ListView具有复选框。对于每一个复选框(他们是约3),它有它特定的AsyncTask。

我从来不知道什么复选框用户选择,所以我不能把在异步任务AlertDialog的结束,因为我从来不知道,如果用户只选择了一个复选框,或两个或三个。

由于AsyncTask的在步骤(仅在第一个异步完成的第二个开始时)执行的,我想过把一切新的AsyncTask与AlertDialog的结束。

 私有类showMessageAsync扩展的AsyncTask<太虚,整数,字符串> {
    @覆盖
    保护字符串doInBackground(空... PARAMS){
        AlertDialog alertDialog;
        alertDialog =新AlertDialog.Builder(getApplicationContext).create();
        alertDialog.setTitle(以下简称程序);
        alertDialog.setIcon(R.drawable.success);
        alertDialog.setCanceledOnTouchOutside(假);
        alertDialog.setMessage(全部完成!);
        alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL,OK,
                              新DialogInterface.OnClickListener(){
            公共无效的onClick(DialogInterface对话,诠释它){
                意图=新的意图(DownloadActivity.this,Menu_activity.class);
                startActivity(A);
                完();
            }
        });
        alertDialog.setOnDismissListener(新DialogInterface.OnDismissListener(){
            @覆盖
            公共无效onDismiss(DialogInterface对话){
                意图=新的意图(DownloadActivity.this,Menu_activity.class);
                startActivity(A);
                完();
            }
        });
        alertDialog.show();
        返回已执行;
    }
}
 

这是错误:

 四月10日至21日:24:34.117:E / AndroidRuntime(1026):致命异常:AsyncTask的
#4月10日至21日04:24:34.117:E / AndroidRuntime(1026):java.lang.RuntimeException的:一个错误而执行发生
doInBackground()4月10号至二十一​​号:24:34.117:E / AndroidRuntime(1026):在
android.os.AsyncTask $ 3.done(AsyncTask.java:299)四月10日至21日:24:34.117:
E / AndroidRuntime(1026):在
java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
4月10号至21日:24:34.117:E / AndroidRuntime(1026):在
java.util.concurrent.FutureTask.setException(FutureTask.java:219)
4月10号至21日:24:34.117:E / AndroidRuntime(1026):在
java.util.concurrent.FutureTask.run(FutureTask.java:239)10-21
04:24:34.117:E / AndroidRuntime(1026):在
android.os.AsyncTask $ SerialExecutor $ 1.运行(AsyncTask.java:230)10-21
04:24:34.117:E / AndroidRuntime(1026):在
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
4月10号至21日:24:34.117:E / AndroidRuntime(1026):在
java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:573)
4月10号至21日:24:34.117:E / AndroidRuntime(1026):在
java.lang.Thread.run(Thread.java:841)4月10日至21号:24:34.117:
E / AndroidRuntime(1026):由:java.lang.RuntimeException的:不能
内螺纹已经不叫尺蠖。prepare创建处理程序()
4月10号至21日:24:34.117:E / AndroidRuntime(1026):在
android.os.Handler< INIT>(Handler.java:197)四月10号至21日:24:34.117:
E / AndroidRuntime(1026):在
android.os.Handler< INIT>(Handler.java:111)四月10号至21日:24:34.117:
E / AndroidRuntime(1026):在android.app.Dialog< INIT>(Dialog.java:107)。
4月10号至21日:24:34.117:E / AndroidRuntime(1026):在
android.app.AlertDialog< INIT>(AlertDialog.java:114)10-21
04:24:34.117:E / AndroidRuntime(1026):在
android.app.AlertDialog $ Builder.create(AlertDialog.java:931)10-21
04:24:34.117:E / AndroidRuntime(1026):在com.example.MyExample.DownloadActivity $ showMessageAsync.doInBackground(DownloadActivity.java:487)
4月10号至21日:24:34.117:E / AndroidRuntime(1026):在com.example.MyExample.DownloadActivity $ showMessageAsync.doInBackground(DownloadActivity.java:1)
4月10日至21日:24:34.117:E / AndroidRuntime(1026):在android.os.AsyncTask $ 2.call(AsyncTask.java:287)4月10日至21日:24:34.117:
E / AndroidRuntime(1026):在java.util.concurrent.FutureTask.run(FutureTask.java:234)10-21
04:24:34.117:E / AndroidRuntime(1026):4 ...更多
 

我把我的AsyncTask是这样的:

 如果(list.get(0).isSelected()==真){
    //名单=类,它包含checkboxs状态
    的String [] PARAMS = {顺序,将String.valueOf(limit_customers)};
    customers.execute(PARAMS);
}
如果(list.get(1).isSelected()==真){
    的String [] PARAMS = {顺序,将String.valueOf(limit_products)};
    products.execute(PARAMS);
}
//等,并在此结束:
showMessageAsync SM =新showMessageAsync();
sM.execute();
 
最详细的 Android Toolbar 开发实践总结

该错误是在这条线:

  alertDialog =新AlertDialog.Builder(getApplicationContext())创建()。
 

解决方案

警告对话框是前景的事情,所以它不能在异步任务后台方法进行。做到这一点 通过这种方式

 私有类showMessageAsync扩展的AsyncTask<字符串,太虚,字符串> {
     AlertDialog alertDialog;
     在preExecute保护无效(){
    super.on preExecute();
            alertDialog =新AlertDialog.Builder(YourClasss.this);
     }
     @覆盖
     保护字符串doInBackground(空... PARAMS){
            返回null;
     }
     @覆盖
     保护无效onPostExecute(字符串结果){
            super.onPostExecute(结果);

            alertDialog.setTitle(以下简称程序);
            alertDialog.setIcon(R.drawable.success);
            alertDialog.setCanceledOnTouchOutside(假);
            alertDialog.setMessage(全部完成!);
            alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL,OK,
                                  新DialogInterface.OnClickListener(){
                                        公共无效的onClick(DialogInterface对话,诠释它){
                                                意图=新的意图(DownloadActivity.this,Menu_activity.class);
                                                startActivity(A);
                                                完();
                                        }
                                    });
            alertDialog.setOnDismissListener(新DialogInterface.OnDismissListener(){
                                  @覆盖
                                  公共无效onDismiss(DialogInterface对话){
                                            意图=新的意图(DownloadActivity.this,Menu_activity.class);
                                            startActivity(A);
                                            完();
                                  }
                    });
            alertDialog.show();
    }

}
 

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

解决方案

alert dialog is foreground thing so it can not be done in background method of async task. Do it by this way

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

}