延迟加载图像有时才有效,有时不起作用



我正在使用基于此问题的延迟加载图像。我正在实现一个搜索函数,当用户输入关键字时,应用程序将执行请求并使用 SAX 分析结果,然后将数据放入 ListView。有时可以将图像加载到 ListView 行的 ImageViews 中,但有时它只会加载一个或两个图像,并且应用程序崩溃。

下面是我的 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
        }
    }
}

编辑
我已经追踪了来自以下PhotoToLoad类的NullPointerException

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

如何解决问题?

似乎您null传递给此方法。确保正确处理null,或者确保不要在那里传递null(首先更好;))。<小时 />您的错误日志指示 NPE 发生在

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

因此,url在这一点上null。所以要么你集成一个支票,比如

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

或者,您可以查看将null传递给此方法的每个调用,使其崩溃。

从 catch() 块中删除返回 null,因为这可能会引发异常并且位图正在获取 NULL 对象。

相关内容

最新更新