【问题标题】:java.lang.NullPointerException in android.util.LruCache.put ( Android )android.util.LruCache.put ( Android ) 中的 java.lang.NullPointerException
【发布时间】:2015-07-11 22:54:16
【问题描述】:

我的应用程序 java.lang.NullPointerException in android.util.LruCache.put

在我的 google 崩溃和 ANR 部分中遇到了这个崩溃异常

我不知道出了什么问题,我确实需要一些帮助,为什么我会得到这个空指针异常以及如何解决它。

崩溃和 ANR:

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

代码:

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);

}

【问题讨论】:

  • 看起来params[0] 可能为空。除此之外,不知道这段代码在做什么。

标签: java android pointers nullpointerexception crash


【解决方案1】:
public static Bitmap createVideoThumbnail (String filePath, int kind)

为视频创建视频缩略图。如果视频损坏或格式不受支持,可能会返回 null

所以,bitmap 的值可能为空。避免这种情况 像这样编写代码:

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

【讨论】:

  • 所以在 doInBackground() 中我只需要添加 (bitmap != null ) ,如果位图等于 null 我应该返回什么?
  • 如果位图为 null,它将返回 null 到 onPostExecute,因为您的 doInBackground 是 Bitmap 类型。
  • 你的回答很清楚,你解决了我的问题,谢谢
【解决方案2】:

这是因为 ThumbnailUtils.createVideoThumbnail() 可以返回 null
因此,您需要在 addBitmapToMemoryCache() 方法中为位图添加检查 NPE

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-01
    • 2015-02-10
    相关资源
    最近更新 更多