2011-05-16 48 views
0

我使用的是基于这个question的延迟加载图像。我正在实现一个搜索功能,当用户输入关键字时,应用程序将执行请求并使用SAX解析结果,然后将数据放入ListView中。有时图像可以加载到ListView行的ImageView中,但有时它只会加载一个或两个图像,并且应用程序崩溃。延迟加载图像有时只有作品,有时它不

下面是我的logcat输出:

05-16 16:37:37.414: ERROR/AndroidRuntime(620): FATAL EXCEPTION: Thread-11 
05-16 16:37:37.414: ERROR/AndroidRuntime(620): java.lang.NullPointerException 
05-16 16:37:37.414: ERROR/AndroidRuntime(620):  at com.TPLibrary.Search.ImageLoader.getBitmap(ImageLoader.java:71) 
05-16 16:37:37.414: ERROR/AndroidRuntime(620):  at com.TPLibrary.Search.ImageLoader.access$0(ImageLoader.java:68) 
05-16 16:37:37.414: ERROR/AndroidRuntime(620):  at com.TPLibrary.Search.ImageLoader$PhotosLoader.run(ImageLoader.java:173) 

下面是其中错误是在存在的:

private Bitmap getBitmap(String url) { // line 68 
    //I identify images by hashcode. Not a perfect solution, but ok for the demo 
    String filename = String.valueOf(url.hashCode()); // line 71 
    File f = new File(cacheDir, filename); 

    //from SD cache 
    Bitmap b = decodeFile(f); 
    if (b != null) 
     return b; 

    //from web 
    try { 
     Bitmap bitmap = null; 
     InputStream is = new URL(url).openStream(); 
     OutputStream os = new FileOutputStream(f); 
     Utils.CopyStream(is, os); 
     os.close(); 
     bitmap = decodeFile(f); 
     return bitmap; 
    } catch (Exception ex){ 
     ex.printStackTrace(); 
     return null; 
    } 
} 

class PhotosLoader extends Thread { 
    public void run() { 
     try { 
      while(true) 
      { 
       //thread waits until there are any images to load in the queue 
       if (photosQueue.photosToLoad.size() == 0) 
        synchronized(photosQueue.photosToLoad) { 
         photosQueue.photosToLoad.wait(); 
        } 
       if (photosQueue.photosToLoad.size() != 0) { 
        PhotoToLoad photoToLoad; 
        synchronized(photosQueue.photosToLoad) { 
         photoToLoad=photosQueue.photosToLoad.pop(); 
        } 
        Bitmap bmp = getBitmap(photoToLoad.url); // line 173 
        cache.put(photoToLoad.url, bmp); 
        Object tag = photoToLoad.imageView.getTag(); 
        if (tag != null && ((String)tag).equals(photoToLoad.url)){ 
         BitmapDisplayer bd = 
          new BitmapDisplayer(bmp, photoToLoad.imageView); 
         Activity a = (Activity)photoToLoad.imageView.getContext(); 
         a.runOnUiThread(bd); 
        } 
       } 
       if (Thread.interrupted()) 
        break; 
      } 
     } catch (InterruptedException e) { 
      //allow thread to exit 
     } 
    } 
} 

编辑
我已经找到了那个来自PhotoToLoadNullPointerException如下:

private class PhotoToLoad { 
    public String url; 
    public ImageView imageView; 
    public PhotoToLoad(String u, ImageView i) { 
     url = u; 
     imageView = i; 
    } 
} 

我该如何解决问题?

回答

3

看来你通过null这种方法。确保你适当地处理null,或确保你不会在那里通过null(首先更好;))。


你的错误日志指出,在NPE发生在

String filename=String.valueOf(url.hashCode()); 

所以,urlnull在这一点上。因此,无论你喜欢集成

if (url == null) { 
    Log.w("null has been passed to getBitmap()"); 
    return null; 
} 

支票或您对每一个地方传递null这种方法使其崩溃呼叫的样子。

+0

你能解释一下吗?我不在你指向的地方 – 2011-05-16 09:54:43

+0

我在帖子中增加了一些解释。我希望现在更清楚。 – Stephan 2011-05-16 09:58:03

+0

嗯..我可以理解升技。但是,你这样做?最后,url仍然是空的?.. – 2011-05-16 10:01:55

0

从catch()块中删除返回null,因为这可能会抛出一个Exception并且Bitmap获取NULL对象。

+0

那么我应该返回什么? – 2011-05-16 09:54:13

+0

取决于你会发生什么。 如果它非常危险,让程序崩溃并显示一条消息告诉开发者。或者你处理它,并给用户一些反馈,他或她做错了什么。有很多不同的可能性... – Beasly 2011-05-16 10:02:25

+0

我想要的是,应用程序将下载图像并将它们放入ImageViews中。而不是崩溃。 – 2011-05-17 07:18:38