我使用的是基于这个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
}
}
}
编辑
我已经找到了那个来自PhotoToLoad
类NullPointerException
如下:
private class PhotoToLoad {
public String url;
public ImageView imageView;
public PhotoToLoad(String u, ImageView i) {
url = u;
imageView = i;
}
}
我该如何解决问题?
你能解释一下吗?我不在你指向的地方 – 2011-05-16 09:54:43
我在帖子中增加了一些解释。我希望现在更清楚。 – Stephan 2011-05-16 09:58:03
嗯..我可以理解升技。但是,你这样做?最后,url仍然是空的?.. – 2011-05-16 10:01:55