AsyncTask 如何工作?

时间:2022-09-06 16:36:24

Could anyone tell me what is going wrong here?

谁能告诉我这里出了什么问题?

public class BackgroungTask extends AsyncTask<String, Void, Void> {

    public Void doInBackground(String... params) {
       //tasks
       return;  //error occurs here!
    }

    public void onPostExecute(Void result) {
        //codes
    }
}

The above class does not depend on return values. So onPostExceute() simply execute codes written in there.

上述类不依赖于返回值。所以onPostExceute()只是执行那里写的代码。

Thanks in advance!

提前致谢!

5 个解决方案

#1


15  

just return null.

只返回null。

public class BackgroungTask extends AsyncTask<String, Void, Void> {

    public Void doInBackground(String... params) {
       //tasks
       return null; 
    }

    public void onPostExecute(Void result) {
        //codes
    }
}

#2


6  

I think you have to use "return null;" instead of just "return;", as Void is a class around the usual "void" value.

我想你必须使用“return null;”而不仅仅是“返回”,因为Void是一个围绕通常的“空白”值的类。

#3


5  

In doInBackGround return null, as Void is an object (does not = void)..

在doInBackGround中返回null,因为Void是一个对象(不= void)。

public class BackgroungTask extends AsyncTask<String, Void, Void> {

public Void doInBackground(String... params) {
   //tasks
   return null;  //error occurs here!
}

public void onPostExecute(Void result) {
    //codes
}
} 

#4


4  

AsyncTask works like this

AsyncTask的工作原理如下

onPreExecute -> doInBackGround -> onPostExecute

onPreExecute - > doInBackGround - > onPostExecute

Neglecting progressUpdates etc, as soon doInBackGround is complete, control goes to onPostExecute. A simple return disrupts this flow hence causing the error. To Solve this, replace with return null Since its a void. The return statement parameters are passed to onPostExecute as parameter, where you can use it to see wheter it has been successful.

忽略progressUpdates等,doInBackGround完成后,控制转到onPostExecute。简单的返回会中断此流程,从而导致错误。要解决此问题,请使用return null替换,因为它是一个void。 return语句参数作为参数传递给onPostExecute,您可以使用它来查看它是否成功。

#5


0  

doInBackGround returns null value

doInBackGround返回null值

public class BackgroungTask extends AsyncTask<String, Void, Void> {
 ....
 ....
}

check this Android dev doc for asynchronous task in andorid

检查这个Android dev文档,以获取andorid中的异步任务

#1


15  

just return null.

只返回null。

public class BackgroungTask extends AsyncTask<String, Void, Void> {

    public Void doInBackground(String... params) {
       //tasks
       return null; 
    }

    public void onPostExecute(Void result) {
        //codes
    }
}

#2


6  

I think you have to use "return null;" instead of just "return;", as Void is a class around the usual "void" value.

我想你必须使用“return null;”而不仅仅是“返回”,因为Void是一个围绕通常的“空白”值的类。

#3


5  

In doInBackGround return null, as Void is an object (does not = void)..

在doInBackGround中返回null,因为Void是一个对象(不= void)。

public class BackgroungTask extends AsyncTask<String, Void, Void> {

public Void doInBackground(String... params) {
   //tasks
   return null;  //error occurs here!
}

public void onPostExecute(Void result) {
    //codes
}
} 

#4


4  

AsyncTask works like this

AsyncTask的工作原理如下

onPreExecute -> doInBackGround -> onPostExecute

onPreExecute - > doInBackGround - > onPostExecute

Neglecting progressUpdates etc, as soon doInBackGround is complete, control goes to onPostExecute. A simple return disrupts this flow hence causing the error. To Solve this, replace with return null Since its a void. The return statement parameters are passed to onPostExecute as parameter, where you can use it to see wheter it has been successful.

忽略progressUpdates等,doInBackGround完成后,控制转到onPostExecute。简单的返回会中断此流程,从而导致错误。要解决此问题,请使用return null替换,因为它是一个void。 return语句参数作为参数传递给onPostExecute,您可以使用它来查看它是否成功。

#5


0  

doInBackGround returns null value

doInBackGround返回null值

public class BackgroungTask extends AsyncTask<String, Void, Void> {
 ....
 ....
}

check this Android dev doc for asynchronous task in andorid

检查这个Android dev文档,以获取andorid中的异步任务