I’ve been scratching my head at this for too long – any insight would be very beneficial.
This is my first time playing with AsyncTask and I’m a bit confused on what I’m doing wrong here – Eclipse is stating: “This method must return a result of type object” However; I can’t see how it’s not – unless I’m completely missing something here.
I’m trying to download an image and display it on imageView1 (Also, if you’re able to save some time and tell me if I’m running my postExecute code correctly or not that would be great 🙂 )
private class DownloadImageTask extends AsyncTask<String, Void, Object> {
protected Object doInBackground(String... urls) {
try {
URL url = new URL(urls[0]);
Object content = url.getContent();
return content;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
protected void onPostExecute(Object result) {
InputStream is = (InputStream) result;
Drawable d = Drawable.createFromStream(is, "src");
ImageView imgView = (ImageView)findViewById(R.id.imageView1);
imgView.setImageDrawable(d);
}
}
You have not returned anything when you are catching exception thats why it is showing message “This method must return a result of type object” .Please return any object or null while catching exception.
Moreover if you are getting the drawable than your postexecute method is right.
Your code should look like this: