延迟加载图像有时有效,有时无效
我正在使用基于此的延迟加载图像
以下是我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来您将
null
传递给了此方法。确保正确处理null
,或者确保不在那里传递null
(第一个更好;))。Your error log indicates, the NPE occurs in
因此,此时
url
为null
。因此,要么集成类似的检查,要么查看将
null
传递给该方法导致其崩溃的每个调用。It seems that you pass
null
to this method. Make sure that you handlenull
appropriately, or make sure you don't passnull
there (first is better ;)).Your error log indicates, the NPE occurs in
So,
url
isnull
at this point. So Either you integrate a check likeor you have a look on every call where you pass
null
to this method making it crash.从 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.