4

Following the tutorial here I have set up an app that should be able to download an image from a URL and place it into an ImageView. The only issue is that it cannot find findViewById because it is in the Activity class instead of the AsyncTask class. code is here:

package com.example.weather2;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageView;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;


public class ImageDownloader
        extends AsyncTask<String, Integer, Bitmap> {
protected void onPreExecute(){
    //Setup is done here
}
@Override
protected Bitmap doInBackground(String... params) {
    //TODO Auto-generated method stub
    try{
        URL url = new URL(params[0]);
        HttpURLConnection httpCon =
                (HttpURLConnection)url.openConnection();
        if(httpCon.getResponseCode() != 200)
            throw new Exception("Failed to connect");
        InputStream is = httpCon.getInputStream();
        return BitmapFactory.decodeStream(is);
    }catch(Exception e){
        Log.e("Image", "Failed to load image", e);
    }
    return null;
}
protected void onProgressUpdate(Integer... params){
    //Update a progress bar here, or ignore it, I didn't do it
}
protected void onPostExecute(Bitmap img){
    ImageView iv = (ImageView) findViewById(R.id.imageView); //here is where the issue is
    if(iv!=null && img!=null){
        iv.setImageBitmap(img);
    }
}
protected void onCancelled(){
}
}

When I comment out the lines with the issue, I don't get any errors, which makes me think it is working, but I can't tell because there is no image being posted. Any help for solving this issue would be much appreciated.

Sam Borick
  • 935
  • 2
  • 10
  • 31

5 Answers5

4

findViewById is a method of Activity class.

And you have

 public class ImageDownloader
    extends AsyncTask<String, Integer, Bitmap>  // not a activity class

If you need the data back in activity you could use interface as a callback to the activity.

I would update ui in Activity itself. look @ blackbelts answer

How do I return a boolean from AsyncTask?

Or

Make AsyncTask an inner class of Activity class and update ui in onPostExecute

Also you might find this blog by Alex Loockwood interesting

http://www.androiddesignpatterns.com/2013/04/retaining-objects-across-config-changes.html

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
3

Instead of calling findViewById() in the AsyncTask, pass in the ImageView as a parameter:

public class ImageDownloader extends AsyncTask<String, Integer, Bitmap> {
    final ImageView mImageView;

    public ImageDownloader(ImageView imageView) {
        mImageView = imageView;
    }

    // ...
laalto
  • 150,114
  • 66
  • 286
  • 303
  • would this be a problem during orientation change? – Raghunandan Jan 06 '14 at 14:22
  • Not really. The ImageView may not be displayed anymore though. In any case, one needs to consider that the views may be recycled (if in an adapter) and the imageview is not the correct one at the time the asynctask completes. For getting something working when working with a tutorial this is ok; for real production use, use a tested imageloader such as Picasso, Volley, Universal-Image-Loader or similar. – laalto Jan 06 '14 at 14:28
  • agreed. I have mostly used UIL for LazyLoading. – Raghunandan Jan 06 '14 at 14:30
1

Use your activity directly when configuring/getting image. This will solve your problem.

public class ImageDownloader extends AsyncTask<String, Integer, Bitmap> {
    private Context context;

    public ImageDownloader(Activity yourActivity) {
        context = activity;
    }

    // ...

    protected void onPostExecute(Void... params){
        ImageView iv = (ImageView) context.findViewById(R.id.imageView); 
        if(iv!=null && img!=null){
            iv.setImageBitmap(img);
        }
    }
}
slhddn
  • 1,917
  • 1
  • 15
  • 21
1

Use cast to AppCompatActivity, such as:

public class ImageDownloader extends AsyncTask<String, Integer, Bitmap> {
private Context context;

public ImageDownloader(Activity yourActivity) {
    context = yourActivity;
}

// ...

protected void onPostExecute(Void... params){
    //Here
    ImageView iv = (ImageView) ((AppCompatActivity) context).findViewById(R.id.imageView); 
    if(iv!=null && img!=null){
        iv.setImageBitmap(img);
    }
}

}

0

You can implement the AsyncTask as a inner class of your activity. And put ImageView iv = (ImageView) findViewById(R.id.imageView); in your activity's onCreate() method. That is what I did in my project.

Or you can pass the activity.this as a parameter to the AsyncTask's constructor. And use ImageView iv = (ImageView)activity. findViewById(R.id.imageView) in your AsyncTask's onPostExecute() method.

FrankSu
  • 331
  • 1
  • 4
  • 10