1

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:

class LoadImage extends AsyncTask<Object, Void, Bitmap>{

    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 && imv != null){
            Log.w("MyApp", "setting bitmap");
            imv.setImageBitmap(result);
        }else{
            Log.w("MyApp", "Nothing happened");
        }
    }

}

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 question. 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.

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)

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.

For reference:

304: if (!imv.getTag().equals(path)) {
305:    Log.w("MyApp", "Not setting images");
306:    return;
307: }
Community
  • 1
  • 1
The Holo Dev
  • 1,016
  • 3
  • 12
  • 22
  • please put the logcat so we can help you more specifically. – Shreyash Mahajan Apr 10 '12 at 06:33
  • Added the logcat and the referenced lines. Hope that helps. Not sure wh the null pointer exception is being thrown though. onPostExecute looks to be in the scope of both imv and path, and I can't see how either would be null. – The Holo Dev Apr 10 '12 at 07:17
  • comment the line 305 and then see what happend ? – Shreyash Mahajan Apr 10 '12 at 07:22
  • I've done that. At that point, I get the same set of images over and over while scrolling, and when stopped, the images start cycling like crazy since multiple async tasks are working on one imageview because of view recycling. The if statement from 304-307 is supposed to help prevent that. That was the goal at least. Instead, I'm watching the same Imageview get set to different images dozens of times after scrolling. – The Holo Dev Apr 10 '12 at 07:28
  • try with clean the project and then run it again – Shreyash Mahajan Apr 10 '12 at 07:31
  • That didn't seem to do anything. – The Holo Dev Apr 10 '12 at 22:37

1 Answers1

1

I found a solution that works for me. I used aQuery, and used the shouldDelay and async set image from file they provide.

if(header.isFile() && !aq.shouldDelay(position, convertView, parent, 
   header.getAbsolutePath())){
     bm = BitmapFactory.decodeFile(header.getAbsolutePath(), options);
     showcase.setImageBitmap(bm);
 } else {                    
     showcase.setImageResource(R.drawable.missing_image);
 }
Jeff Axelrod
  • 27,676
  • 31
  • 147
  • 246
The Holo Dev
  • 1,016
  • 3
  • 12
  • 22