从 Google's Android Documentation 可以看出我们看到了
Params, the type of the parameters sent to the task upon execution.
Progress, the type of the progress units published during the background computation.
Result, the type of the result of the background computation.
也在this中提到很好的答案。
但是,为什么将 Void
作为结果类型会导致 Android Studio 在 doInBackground
行发出以下警告:
doInBackground(String...)' in '(..)MainActivity.myTask' clashes with 'doInBackground(Params...)' in 'android.os.AsyncTask'; attempting to use incompatible return type
AsyncTask
是这样声明的:
private class myTask extends AsyncTask<String,Void,Void> {
@Override
protected void doInBackground(String... url) {
//do stuff with url[0]
}
@Override
protected void onPostExecute() {
//more stuff
}
}
相比之下,这似乎工作正常:
private class myTask extends AsyncTask<String,Void,String> {
@Override
protected String doInBackground(String... url) {
//do stuff with url[0]
return "hi";
}
@Override
protected void onPostExecute(String result) {
//more stuff
}
}
请注意,关于这个主题有各种各样的问题,但大多数只是搞乱了类中的 AsyncTask
参数或参数和返回值。
最佳答案
试试这段代码
private class myTask extends AsyncTask<String,Void,Void> {
@Override
protected Void doInBackground(String... url) {
//do stuff with url[0]
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
//more stuff
}
}
关于android - `doInBackground()` 不接受类型为 void 的 `AsyncTask`?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38993601/