PHPirate PHPirate - 3 months ago 28
Android Question

A `doInBackground()` of type void is not accepted by `AsyncTask`?

As seen from Google's Android Documentation we see that


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.


also mentioned in this excellent answer.

But then, why does having
Void
as result type result in the following warning by Android Studio on the
doInBackground
line:


doInBackground(String...)' in '(..)MainActivity.myTask' clashes with 'doInBackground(Params...)' in 'android.os.AsyncTask'; attempting to use incompatible return type


This is how the
AsyncTask
is declared:

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
}
}


In comparison, this seems to work fine:

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
}
}


Note there are various questions about this topic, but most of them just messed up the
AsyncTask
parameters or parameters and return values in the class.

Answer

try this code

 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
    }
}