views:

90

answers:

1

Hey guys,

I'm using a lazy image loader for my ListView with the following code inside getView():

Bitmap cachedImage = asyncImageLoader.loadBitmap(item.getImage(), wallpaperNumber, new ImageCallback()
{
    public void imageLoaded(Bitmap bitmapImage, String wallpaperNumber)
    {
        ImageView imageViewByTag = (ImageView) listView.findViewWithTag(wallpaperNumber);
        imageViewByTag.setImageBitmap(bitmapImage);
    }
});

And the following inside the AsyncImageLoader class:

public Bitmap loadBitmap(final byte[] image, final String wallpaperNumber, final ImageCallback imageCallback) 
{
    if (imageCache.containsKey(wallpaperNumber)) 
    {
        SoftReference<Bitmap> softReference = imageCache.get(wallpaperNumber);
        Bitmap Bitmap = softReference.get();

        if (Bitmap != null) 
        {
            return Bitmap;
        }
    }
    final Handler handler = new Handler() 
    {
        @Override
        public void handleMessage(Message message) 
        {
            try
            {
                if (message.obj != null)
                {                       
                    imageCallback.imageLoaded((Bitmap) message.obj, wallpaperNumber);
                }
                else
                {
                    Bitmap bitmapImage = loadImage(image);
                    imageCache.put(wallpaperNumber, new SoftReference<Bitmap>(bitmapImage));

                    imageCallback.imageLoaded(bitmapImage, wallpaperNumber);
                }
            } 
            catch (Exception e) 
            {
                MiscFunctions.logStackTrace(e);
            }
        }
    };
    new Thread() 
    {
        @Override
        public void run() 
        {
            Bitmap bitmapImage = loadImage(image);
            imageCache.put(wallpaperNumber, new SoftReference<Bitmap>(bitmapImage));
            Message message = handler.obtainMessage(0, bitmapImage);
            handler.sendMessage(message);
        }
    }.start();
    return null;
}

private static Bitmap loadImage(byte[] image) 
{
    Bitmap bitmapImage = BitmapFactory.decodeByteArray(image, 0, image.length);
    bitmapImage = Bitmap.createScaledBitmap(bitmapImage, imageWidth, imageHeight, true);

    return bitmapImage;
}

public interface ImageCallback 
{
    public void imageLoaded(Bitmap imageBitmap, String wallpaperNumber);
}

For an unknown reason to me, after to switched from using the common SQLiteOpenHelper to a custom one which allows me to store my database on the SD card, every 6th or 7th image that is loaded throws a NullPointer at:

imageCallback.imageLoaded((Bitmap) message.obj, wallpaperNumber);

I've checked everything and don't actually know what variable is actually null.

Any help?

A: 

I've checked everything and don't actually know what variable is actually null.

Put in a couple of assert statements to check for null (of imageCallback, message, message.obj, wallpaperNumber).

Thilo
Does Android support assertions? If not, just regular if statements.
Thilo
@Thilo Not sure about assertions... but I have tried if statements for each of the variables, no issues at all.
mlevit
for all four? Are you sure the exception happens on this line? Not inside imageLoaded for example.
Thilo
@Thilo This is a screenshot of the stack trace: http://dl.dropbox.com/u/2045858/Images/stackoverflow_3895276.png. Line 49 is `imageCallback.imageLoaded((Bitmap) message.obj, wallpaperNumber);`
mlevit
Yes, but there is one more stackframe. Your NPE occurs inside of imageLoaded, at HistoryViewActivity:439. Maybe `listView.findViewWithTag(wallpaperNumber);` returns null?
Thilo
@Thilo is right. The NPE is clearly being thrown in the anonymous class. But it could also be that `listView` is null. The line number should allow the OP to tell which is the case.
Stephen C
@Thilo You're correct. It's returning null. Just before `Bitmap cachedImage`... I do: `imageView.setTag(wallpaperNumber);` How is it still returning a null?
mlevit
By the way, it only happens when I flick to scroll. If I scroll slowly and smoothly then it doesn't occur.
mlevit
Sounds like a classic race condition to me
Falmarri