thest1 / LazyList

Lazy load of images in Android
http://stackoverflow.com/questions/541966/android-how-do-i-do-a-lazy-load-of-images-in-listview/3068012#3068012
MIT License
1.2k stars 497 forks source link

imageView.getContext() can fail - is there another way to get the activity? #17

Closed Niksac closed 11 years ago

Niksac commented 12 years ago

Hi

I love the lazylist and in particular the ImageLoader. It worked fine for me until i tried something new :

I created a horizontal Scroller and loaded views into it within the oncreate code oh my activity. After creating a such ImageView (which is in a layout which i add to the horizontal scroller) i used DisplayImage(img, iv) to get a picture from an url into it.

Well it never worked .. no exceptions, no result at all. I analyzed all the ImageLoader code and used Logcat to find the command which caused the behavior.

The issue is in the run method of the PhotosLoader class which will try to get the activity the imageView lives in to let its UI Thread set the image bitmap:

(Activity)photoToLoad.imageView.getContext();

Based on Logcat outputs i think my view will return an application object in this case:

08-24 12:54:30.895: V/REPMKT(2430): android.app.Application@414626f0

I fixxed the issue by passing my activity into the ImageLoader via constructor and from there pass it into the PhotosLoader. I don't hink that's a clever approach but it works.

Any ideas how to fix this ?

Nik

JackWS commented 12 years ago

I just started on this yesterday. I see what you mean about context. Care to share your code?

This repo might provide some insight too.

https://github.com/kaeppler/ignition.git

thest1 commented 12 years ago

You say "loaded views into it within the oncreate code oh my activity". Can you show this code please? I mean code where you create ImageView or inflate it from xml. I ask because I suspect you pass application context when you create imageviews. You should pass activity context. I faced something like that before.

hardaudit commented 11 years ago

this error appeared because all activities are contexts, but not all contexts are activities. I think the ImageView was not created from an activity.

thest1 commented 11 years ago

It's already fixed in the latest sources. I don't use getContext() any more.