Reputation: 5764
I download a JSON string that includes background image URL of a View. Download code is inside doInBackground(Object... params)
. For loading images, I am using Universal Image Loader.
ImageLoader.getInstance().loadImage(shop.background, new ImageLoadingListener() {
public void onLoadingStarted(String imageUri, View view) {
// TODO Auto-generated method stub
}
public void onLoadingFailed(String imageUri, View view,
FailReason failReason) {
// TODO Auto-generated method stub
}
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
// TODO Auto-generated method stub
Drawable d = new BitmapDrawable(getResources(),loadedImage);
mainLayout.setBackgroundDrawable(d);
}
public void onLoadingCancelled(String imageUri, View view) {
// TODO Auto-generated method stub
}
});
I am getting
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
Where am I wrong?
Upvotes: 1
Views: 499
Reputation: 1015
Try with this.
runOnUiThread(new Runnable() {
@Override
public void run() {
mainLayout.setBackgroundDrawable(d);
}
});
Upvotes: 0
Reputation: 10518
Move ImageLoader's code to onPostExecute of your asyncTask. You cant create handler in any thread (ImageLoader needs a handler I suppose). You need a thread with looper and Looper.prepare() being called. AsyncTask's threads dont have looper. So move code to onPostExecute to run it on main thread.
Upvotes: 1
Reputation: 13705
You are wrong in the point where you are trying to create a handler from a worker thread that has not called Looper.prepare(), maybe you didn't even deliberately do it, however there's plenty of methods that create handlers in order to modify Views, usually this handlers attach to the main UI Thread, however is very likely that you are calling a method trying to create the handler from a Worker Thread, If so, what you have to do is, calling that method from main UI, but make sure it will not froze your UI, if that's the case create your own handler and execute it once you are done, or call the runOnUIThread method if you are in an Activity when needed...
Hope this Helps.
Regards!
Upvotes: 0