代码之家  ›  专栏  ›  技术社区  ›  mlevit

未知引发NullPointerException的原因

  •  1
  • mlevit  · 技术社区  · 15 年前

    我正在为我的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个图像都会在以下位置抛出一个空指针:

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

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

    有什么帮助吗?

    1 回复  |  直到 15 年前
        1
  •  0
  •   Thilo    15 年前

    assert imageCallback, message, message.obj, wallpaperNumber