2010-10-09 45 views
1

我用我的ListView用下面的代碼懶圖像加載器內getView()未知爲何拋出NullPointerException

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

我檢查了一切,實際上並不知道什麼變量實際上是null。

放入幾個assert語句來檢查空(imageCallback, message, message.obj, wallpaperNumber)。

+0

Android是否支持斷言?如果不是,只是定期的if語句。 – Thilo 2010-10-09 01:26:13

+0

@Thilo不確定關於斷言......但我已經嘗試瞭如果每個變量的語句,根本沒有問題。 – mlevit 2010-10-09 01:46:41

+0

所有四個?你確定這一行發生異常嗎?例如,不在imageLoaded中。 – Thilo 2010-10-09 01:50:17

相關問題