简体   繁体   中英

java.lang.NullPointerException in android.util.LruCache.put ( Android )

I'm getting this crash exception in my google Crashes & ANRs section for my app java.lang.NullPointerException in android.util.LruCache.put

I have no idea what's wrong I do need some help please, why I do get this null pointer exception and how to fix it.

Crashes & ANRs:

java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.NullPointerException: key == null || value == null
at android.util.LruCache.put(LruCache.java:167)
at 
com.b3du.im.GridAdapter.addBitmapToMemoryCache(GridAdapter.java:77)
at com.b3du.im.GridAdapter$BitmapWorkerTaskVideo.doInBackground(GridAdapter.java:218)
at com.b3du.im.GridAdapter$BitmapWorkerTaskVideo.doInBackground(GridAdapter.java:205)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
... 4 more

Code:

public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
    if (getBitmapFromMemCache(key) == null) {
        mMemoryCache.put(key, bitmap);
    }
}

public Bitmap getBitmapFromMemCache(String key) {
    return mMemoryCache.get(key);
}

class BitmapWorkerTaskVideo extends AsyncTask<String, Void, Bitmap> {
    private final WeakReference<ImageView> imageViewReference;

    public BitmapWorkerTaskVideo(ImageView imageView) {
        // Use a WeakReference to ensure the ImageView can be garbage collected
        imageViewReference = new WeakReference<ImageView>(imageView);
    }

    // Decode image in background.
    @Override
    protected Bitmap doInBackground(String... params) {
        final Bitmap bitmap = decodeSnapshotFromFileVideo(params[0], 100, 100);
        addBitmapToMemoryCache(String.valueOf(params[0]), bitmap);
        return bitmap;
    }

    // Once complete, see if ImageView is still around and set bitmap.
    @Override
    protected void onPostExecute(Bitmap bitmap) {
        if (imageViewReference != null && bitmap != null) {
            final ImageView imageView = imageViewReference.get();
            if (imageView != null) {
                imageView.setImageBitmap(bitmap);
            }
        }
    }

public Bitmap decodeSnapshotFromFileVideo (String filepath, int reqWidth, int reqHeight) {

    //Create a file, using the filepath
    File file =  new File (filepath);
    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
   ThumbnailUtils.createVideoThumbnail(file.getAbsolutePath(), MediaStore.Video.Thumbnails.MICRO_KIND);
    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    options.inJustDecodeBounds = false;
    return  ThumbnailUtils.createVideoThumbnail(file.getAbsolutePath(), MediaStore.Video.Thumbnails.MICRO_KIND);

}
public static Bitmap createVideoThumbnail (String filePath, int kind)

Create a video thumbnail for a video. May return null if the video is corrupt or the format is not supported.

So, Might be bitmap value is null. to avoid this Write your code like this:

if(bitmap != null)
{
addBitmapToMemoryCache(String.valueOf(params[0]), bitmap);
}

It's because ThumbnailUtils.createVideoThumbnail() can return null
So you will need to add check NPE for the bitmap in addBitmapToMemoryCache() method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM