Note that there are some explanatory texts on larger screens.

plurals
  1. POLoading images in listview
    primarykey
    data
    text
    <p>I'm attempting to load images in to display next to text in a list view. Since scrolling is terrible without an asynctask, I'm attempting to implement that. Unfortunately, my code is giving me null pointer errors in onPostExecute() and I cannot seem to find out why. My code is as follows:</p> <pre><code>class LoadImage extends AsyncTask&lt;Object, Void, Bitmap&gt;{ private ImageView imv; private Object imageViewHolder; private String position; private Object path; public LoadImage(ImageView imv, String _position) { Log.w("MyApp", "creating LoadImage"); this.imv = imv; Log.w("MyApp", "got image view"); path = imv.getTag(); Log.w("MyApp", "Got Imageview Path"); position = _position; Log.w("MyApp", "LoadImage created"); } @Override protected Bitmap doInBackground(Object... params) { Log.w("MyApp", "in doInBackground"); BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 2; Log.w("MyApp", "made bitmap factory"); Bitmap bm = null; Log.w("MyApp", "Getting URL ID"); File dir = new File(PATH + position); Log.w("MyApp", "Have URL ID"); if(dir.isDirectory()){ Log.w("MyApp","Dir is a directory"); File header = new File(dir.getAbsolutePath() + "/title"); Log.w("MyApp", "Checking for title"); if(header.isFile()){ bm = BitmapFactory.decodeFile(header.getAbsolutePath(), options); } if(bm!=null){ Log.w("MyApp", "Title is good"); }else{ Context c = ReadingList.this; bm = BitmapFactory.decodeResource(c.getResources(), R.drawable.ic_menu_gallery); } }else{ Log.w("MyApp", "Dir Not Directory"); Context c = ReadingList.this; bm = BitmapFactory.decodeResource(c.getResources(), R.drawable.ic_menu_gallery); } return bm; } @Override protected void onPostExecute(Bitmap result) { if (!imv.getTag().equals(path)) { Log.w("MyApp", "Not setting images"); return; } if(result != null &amp;&amp; imv != null){ Log.w("MyApp", "setting bitmap"); imv.setImageBitmap(result); }else{ Log.w("MyApp", "Nothing happened"); } } } </code></pre> <p>The problem I'm fighting with is not letting bitmaps be recycled along with the recycled view. I've looked at some examples on this site, and am not sure why this isn't working. A lot of code was borrowed from the response to this <a href="https://stackoverflow.com/questions/7729133/using-asynctask-to-load-images-in-listview">question</a>. The onPostExecute doesn't input anything into the log, nor does it crash immediatly. It seems to take about two minutes to crash. The images are being pulled from the SD card, so I'm not sure why it's taking so long to decide if it wants to crash or not.</p> <pre><code>in doInBackground made bitmap factory Getting URL ID Have URL ID Dir is a directory Checking for title Title is good in doInBackground made bitmap factory getting URL ID Have URL ID Dir is a directory Checking title Shutting down VM threadid=1: thread exiting with uncaught exception (group=0x409c01f8) FATAL EXCEPTION: main java.lang.NullPointerException at com.theholodev.glowreader.ReadingList$LoadImage.onPostExecute(ReadingList.java:305) at com.theholodev.glowreader.ReadingList$LoadImage.onPostExecute(ReadingList.java:1) </code></pre> <p>It goes on to repeat the first 7 lines multiple times after this, but the app has crashed and I cannot see if they do anything. onPostExecute does not ever seem to be called again however.</p> <p>For reference:</p> <pre><code>304: if (!imv.getTag().equals(path)) { 305: Log.w("MyApp", "Not setting images"); 306: return; 307: } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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