如何从另一个AsyncTask调用AsyncTask?

时间:2016-04-24 23:02:50

标签: android android-asynctask

我有两个AsyncTask:首先查找素数,如果成功,我必须调用显示“Yop!”字样的第二个AsyncTask。 (将此单词添加到数组列表中,并在AsyncTask中显示三个)。

如果我从onProgressUpdate调用第二个AsyncTask,我有错误:

Process: com.egoriku.asynctask, PID: 3274
                                                                     java.lang.IllegalStateException: Cannot execute task: the task has already been executed (a task can be executed only once)
                                                                         at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:605)
                                                                         at android.os.AsyncTask.execute(AsyncTask.java:560)
                                                                         at com.egoriku.asynctask.activities.MainActivity$MyTaskTwo.onProgressUpdate(MainActivity.java:175)
                                                                         at com.egoriku.asynctask.activities.MainActivity$MyTaskTwo.onProgressUpdate(MainActivity.java:153)
                                                                         at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:680)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                         at android.os.Looper.loop(Looper.java:148)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:6066)
                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:770)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:660)

MyTaskOne:

private class MyTaskTwo extends AsyncTask<Void, Integer, Void> {

    public MyTaskTwo() {
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        Log.e("T2", "onProgressUpdate");
        super.onProgressUpdate(values);
        tempListMessages.add(hintThreadTwo + " " + String.valueOf(values[0]));
        Log.e("T2", "add to list");
        myTaskFour.execute();
        Log.e("T2", "tast 3 execute");
        Log.e("T2", "end");
    }

    @Override
    protected Void doInBackground(Void... params) {
        Log.e("T2", "doInBackground");
        int nowDigit = 1;
        while (!isCancelled()) {
            boolean flag = true;

            for (int i = 2; i < nowDigit; i++) {
                if (nowDigit % i == 0) {
                    flag = false;
                }
            }

            if (flag) {
                try {
                    Thread.sleep(450);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                callThreadFour = true;
                publishProgress(nowDigit);
            }
            nowDigit++;
        }
        return null;
    }
}

MyTaskTwo

rivate class MyTaskFour extends AsyncTask<Void, Void, Void> {
    public MyTaskFour() {
    }

    @Override
    protected Void doInBackground(Void... params) {
        Log.e("T4", "doInBackground");
        publishProgress();
        return null;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        Log.e("T4", "onPostExecute");
        super.onPostExecute(aVoid);
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        Log.e("T4", "onProgressUpdate");
        super.onProgressUpdate(values);
        tempListMessages.add(hintThreadFour + " " + textThreadFour);
        callThreadFour = false;
    }
}

2 个答案:

答案 0 :(得分:2)

myTaskFour对象已在代码的某处执行。尝试将onProgressUpdate()类的MyTaskTwo方法更改为此类

@Override
protected void onProgressUpdate(Integer... values) {
    Log.e("T2", "onProgressUpdate");
    super.onProgressUpdate(values);
    tempListMessages.add(hintThreadTwo + " " + String.valueOf(values[0]));
    Log.e("T2", "add to list");
    new MyTaskFour.execute(); // creates a new MyTaskFour instance
    Log.e("T2", "tast 3 execute");
    Log.e("T2", "end");
}

答案 1 :(得分:0)

后台线程永远不应该更新UI。看起来MyTaskFour不适合AsyncTask。 AsyncTask.onPublishProgress已经在UI线程上了,您只需将myTaskFour.execute();替换为publishProgress();并完全删除MyTaskFour。