我在我的ListView中使用了一个惰性图像加载器,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);
}
});
以下AsyncImageLoader
类:
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);
}
对于我未知的原因,在使用公共SQLiteOpenHelper
切换到允许我将数据库存储在SD卡上的自定义文件后,加载的每个第6或第7个图像都会抛出NullPointer :
imageCallback.imageLoaded((Bitmap) message.obj, wallpaperNumber);
我已经检查了所有内容,实际上并不知道哪个变量实际为空。
任何帮助?
答案 0 :(得分:0)
我已经检查了所有内容,实际上并不知道哪个变量实际为空。
输入几个assert
语句来检查null(imageCallback, message, message.obj, wallpaperNumber
)。