JoeHz JoeHz - 1 year ago 101
Android Question

Android ASyncTask Return type mismatch

I'm pulling my hair out with what looks to be a really simple thing to do. Surely this is just a typo or something stupid, but I'm just not seeing it...

I have an ASyncTask class defined as such (and it compiles fine)

public class SectionRetrieval extends AsyncTask<Void, Void, List<Section>>

protected List<Section> doInBackground(Void... voids)
List<Section> sections = new ArrayList<Section>();


return sections;

In my app I attempt to call it as such

List<Section> sections = new SectionRetrieval().execute();

But this doesn't compile

Error:(472, 63) Gradle: error: incompatible types:

AsyncTask<Void,Void,List<Section>> cannot be converted to List<Section>

I've tried separating the creation of the new SectionalRetrieval instance and the call the .execute(). No such luck. It really does seem to think execute() is really trying to return an instance of SectionalRetrieval.

So how did I make it think that's what I wanted?

Answer Source

You receive whatever you return from doInBackground in the parameter of onPostExecute.

You update the UI from onPostExecute as well.

You could do execute().get() to have it compile, probably, but I wouldn't recommend that

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download