Note that there are some explanatory texts on larger screens.

plurals
  1. POLazy loading images works only sometimes, sometimes it doesn't
    primarykey
    data
    text
    <p>I'm using lazy loading images based on this <a href="https://stackoverflow.com/questions/541966/android-how-do-i-do-a-lazy-load-of-images-in-listview">question</a>. 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. </p> <p>Below is my LogCat output: </p> <pre><code>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) </code></pre> <p>Below is where the errors are occuring at:</p> <pre><code>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 &amp;&amp; ((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 } } } </code></pre> <p><strong>EDIT</strong><br> I have tracked down the <code>NullPointerException</code> that comes from the <code>PhotoToLoad</code> class below:</p> <pre><code>private class PhotoToLoad { public String url; public ImageView imageView; public PhotoToLoad(String u, ImageView i) { url = u; imageView = i; } } </code></pre> <p>How can I solve the problem?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload