Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 快速异步任务DoinBackground Q_Android - Fatal编程技术网

Android 快速异步任务DoinBackground Q

Android 快速异步任务DoinBackground Q,android,Android,说我在跑步 new CommentList().execute(url); 如果我在doInBackground方法中,我捕获了一个空值,并且在该空值异常中,我尝试再次运行相同的异常: new CommentList().execute(url); 它会停止运行第一个吗 我可以这样做吗: if (result == null) { cancel(true); } @Override protected void

说我在跑步

new CommentList().execute(url);
如果我在doInBackground方法中,我捕获了一个空值,并且在该空值异常中,我尝试再次运行相同的异常:

new CommentList().execute(url);
它会停止运行第一个吗

我可以这样做吗:

if (result == null) {
    cancel(true);                           
    }

@Override
    protected void onCancelled() {
        new CommentList().execute(commentlinkurl);
    }

基本上,如果onPostExecute被取消,我不希望它运行。

这不是一个好主意。您不应该在非UI线程上创建新任务(从
doInBackground
方法中)

发件人:

必须遵守一些线程规则 要使该类正常工作,请遵循以下步骤:

  • 必须在UI线程上创建任务实例。
  • 必须在UI线程上调用执行(参数…。
  • 不要手动调用onPreExecute()、onPostExecute(结果)、doInBackground(参数…)和onProgressUpdate(进度…)
  • 该任务只能执行一次(如果尝试第二次执行,将引发异常)
根据评论进行编辑: 但是,您可以在
onPostExecute
onCancelled
方法中再次启动任务。您只需从
doInBackground
返回一些特定的结果,或者将您的Throwable保存在AsyncTask成员变量中,以进一步分析它:

protected void onPostExecute(Something something) {
    if(something == null){
        // safe to start new execute task here
    }
    // or
    if(mException instanceof TemporaryIssueException){
        // safe to start new execute task here
    }
}

这不是一个好主意。您不应该在非UI线程上创建新任务(从
doInBackground
方法中)

发件人:

必须遵守一些线程规则 要使该类正常工作,请遵循以下步骤:

  • 必须在UI线程上创建任务实例。
  • 必须在UI线程上调用执行(参数…。
  • 不要手动调用onPreExecute()、onPostExecute(结果)、doInBackground(参数…)和onProgressUpdate(进度…)
  • 该任务只能执行一次(如果尝试第二次执行,将引发异常)
根据评论进行编辑: 但是,您可以在
onPostExecute
onCancelled
方法中再次启动任务。您只需从
doInBackground
返回一些特定的结果,或者将您的Throwable保存在AsyncTask成员变量中,以进一步分析它:

protected void onPostExecute(Something something) {
    if(something == null){
        // safe to start new execute task here
    }
    // or
    if(mException instanceof TemporaryIssueException){
        // safe to start new execute task here
    }
}

+1但可能会将相关结果返回到
onPostExecute(…)
,并创建一个新实例并从中执行,这样就可以了。onCancelled也在UI线程上运行,因此可以在其中启动一个新任务。调用“cancel”将导致在doInBackground(Object[])返回后在UI线程上调用onCancelled(Object)。调用此方法可确保永远不会调用onPostExecute(对象)。UI线程上是否有OnCancelled()?如果没有,当我捕获空值时,我如何跳转到onPostExecute?嗯,如果我在doInBackground中发生空值之前捕获空值,那么doInBackground将继续执行?+1,但可能会将相关结果返回到
onPostExecute(…)
创建一个新实例并从中执行也可以。onCancelled也在UI线程上运行,因此在其中启动一个新任务应该可以。调用“cancel”将导致在doInBackground(Object[])返回后在UI线程上调用onCancelled(Object)。调用此方法可确保永远不会调用onPostExecute(对象)。UI线程上是否有OnCancelled()?如果没有,当我捕捉到空值时,如何跳转到onPostExecute?嗯,那么如果我在doInBackground中发生空值之前捕捉到空值,那么doInBackground将继续?