【问题标题】:Android AsyncTask's doInBackground not startedAndroid AsyncTask 的 doInBackground 未启动
【发布时间】:2016-05-25 13:57:25
【问题描述】:

我的一个班级中有一个 AsyncTask。我正在执行 AsyncTask,例如:

mAsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

当我调试我的应用程序时,正确调用了 mAsyncTask 的 onPreExecute() 函数,但未调用 doInBackground 函数。 什么可能导致问题?

PlanRouteAsync 代码:-

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

    @Override
    protected void onPreExecute(){
        super.onPreExecute();
        mIsRunning = true;
    }
    @Override
    protected Void doInBackground(Void... params){
        ...
        return null;
    }
    @Override
    protected void onProgressUpdate(Integer... values){
        super.onProgressUpdate(values);
        ...
    }
    @Override
    protected void onCancelled(){
        super.onCancelled();
        ...
    }
    @Override
    protected void onCancelled(Void aVoid){
        super.onCancelled(aVoid);
        ....
    }
    @Override
    protected void onPostExecute(Void aVoid){
        super.onPostExecute(aVoid);
        ....
    }
}

这是一个公司应用程序,有几个 AsyncTask 在后台运行,但我读过,executeOnExecutor 函数应该并行执行 asycnTask。

我也阅读了这些帖子,但它们没有帮助:

Android SDK AsyncTask doInBackground not running (subclass)

Android AsyncTask not working

编辑:

我的 build.gradle 文件:

android {
    compileSdkVersion 23
    buildToolsVersion '23.0.3'

    defaultConfig {
        minSdkVersion 18
        targetSdkVersion 23
    }
}

dependencies {
    compile project(':mPChartLib')
    compile project(':sVGSupport')
    compile 'com.google.android.gms:play-services:9.0.0'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'io.github.luizgrp.sectionedrecyclerviewadapter:sectionedrecyclerviewadapter:1.0.4'
}

【问题讨论】:

  • 你的targetSdkVersion 是什么?
  • 不是您问题的直接解决方案,但我建议一般远离 AsyncTask,因为它缺乏与 Activity 生命周期的集成,而且它在 sdk 版本之间的行为不同。还有其他几个更好的解决方案,包括我最喜欢的 RxJava
  • 谢谢,我会看看,但我真的不想重写完整的代码:/。您如何看待线程池?
  • 您的代码 sn-p 不包含任何信息。你的 onPreExecute()doInBackground() 里面有什么?
  • 我已经添加了我的 onPreExecute() 函数,doInBackground() 无关紧要,因为它根本没有运行。

标签: android multithreading asynchronous android-asynctask


【解决方案1】:

我解决了这个问题,我在后台还有另一个 Asynctask,它也像 mAsyncTask2.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); 一样执行。由于特殊原因,此 AsyncTask 从未完成它的 doInBackground ,这导致了问题。

所以我用一个新线程替换了另一个 AsyncTask,它现在可以工作了。 感谢您的建议!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多