Issue
I am trying to perform a very simple task (loading image to ImageView in the background), but can't make it to work. Any help much appreciated.
Here is what I have so far: This is how I call the class in on create in the main thread:
LoadImage newImage=new LoadImage();
newImage.execute(myImgeView);
Next I created the class:
public class LoadImage extends AsyncTask<ImageView, Void, ImageView> {
ImageView imageView;
@Override
protected ImageView doInBackground(ImageView... params) {
Log.e("myTag",": Can it see this class from where I call it? Yes it does" );
imageView.setImageResource(R.drawable.myNewImage);
return imageView;
}
}
I can't figure out how to set it up correctly. It crashes saying something like: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference.
Thanks in advance for your help.
Solution
The problem lies in this code:
imageView.setImageResource(R.drawable.myNewImage);
The variable imageView can't be seen from the AsyncTask Class, the imageView you want is inside the params array so you can reference it with params[0]. So replace the above code with:
ImageView imageView = params[0];
imageView.setImageResource(R.drawable.myNewImage);
return imageView;
Answered By - Kidus
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.