【问题标题】:Unknown why the NullPointerException is thrown未知为什么会抛出 NullPointerException
【发布时间】:2010-10-09 01:08:50
【问题描述】:

我正在为我的 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);

我已经检查了所有内容,但实际上并不知道什么变量实际上是 null。

有什么帮助吗?

【问题讨论】:

    标签: java android listview nullpointerexception


    【解决方案1】:

    我已经检查了所有内容,但实际上并不知道什么变量实际上是 null。

    放入几个assert 语句来检查空值(imageCallback, message, message.obj, wallpaperNumber)。

    【讨论】:

    • Android 支持断言吗?如果没有,只需常规 if 语句。
    • @Thilo 不确定断言...但我已经尝试了每个变量的 if 语句,完全没有问题。
    • 四个?你确定异常发生在这条线上吗?例如,不在 imageLoaded 内部。
    • 是的,但是还有一个堆栈帧。您的 NPE 发生在 imageLoaded 内,位于 HistoryViewActivity:439。也许listView.findViewWithTag(wallpaperNumber); 返回null?
    • 对我来说听起来像是经典的比赛条件
    猜你喜欢
    • 2014-12-18
    • 2014-07-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多