延迟加载图像有时有效,有时无效

发布于 2024-11-07 14:51:33 字数 3041 浏览 0 评论 0原文

我正在使用基于此的延迟加载图像

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

我该如何解决这个问题?

I'm using lazy loading images based on this question. I'm implementing a search function that, when the user enters a keyword, the application will do a request and parse the result using SAX and then place the data into a ListView. Sometimes the images can be loaded into ImageViews of the ListView rows but sometimes it will load only one or two images and the application crashes.

Below is my LogCat output:

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)

Below is where the errors are occuring at:

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
        }
    }
}

EDIT
I have tracked down the NullPointerException that comes from the PhotoToLoad class below:

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

How can I solve the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

叫思念不要吵 2024-11-14 14:51:33

看来您将 null 传递给了此方法。确保正确处理 null,或者确保不在那里传递 null(第一个更好;))。


Your error log indicates, the NPE occurs in

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

因此,此时 urlnull。因此,要么集成类似的检查

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

,要么查看将 null 传递给该方法导致其崩溃的每个调用。

It seems that you pass null to this method. Make sure that you handle null appropriately, or make sure you don't pass null there (first is better ;)).


Your error log indicates, the NPE occurs in

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

So, url is null at this point. So Either you integrate a check like

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

or you have a look on every call where you pass null to this method making it crash.

不乱于心 2024-11-14 14:51:33

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

Remove return null from your catch() block because this might be throwing an Exception and the Bitmap is getting NULL object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文