Sam Borick
Sam Borick

Reputation: 955

Android: cannot resolve method 'findViewById(int)' in AsyncTask

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.

Upvotes: 4

Views: 34717

Answers (5)

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

}

Upvotes: 1

slhddn
slhddn

Reputation: 1997

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

Upvotes: 1

Raghunandan
Raghunandan

Reputation: 133560

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

Upvotes: 4

FrankSu
FrankSu

Reputation: 331

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.

Upvotes: 0

laalto
laalto

Reputation: 152807

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

    // ...

Upvotes: 3

Related Questions