android.util.LruCache.put 中的 java.lang.NullPointerException ( Android )

标签 java android pointers nullpointerexception crash

我在我的应用程序的 Google 崩溃和 ANR 部分中遇到此崩溃异常 android.util.LruCache.put 中的 java.lang.NullPointerException

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

崩溃和 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);

}

最佳答案

public static Bitmap createVideoThumbnail (String filePath, int kind)

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

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

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

关于android.util.LruCache.put 中的 java.lang.NullPointerException ( Android ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31362653/

相关文章:

循环的 Java 单元测试帮助

java - 在文件上使用扫描仪并保存在阵列上

C++:指针作为哈希表中的键

c - 未分配的字符串如何存储在内存中以及如何引用它?

c - 插入二叉树时出现段错误

java - body 在不施加任何力的情况下移动? (方框2d)

java - 如何在 2D 正方形中移动对象

java - 有关创建只读 Android 应用程序的两部分查询

android - 如何从源代码中隐藏 AdMob 应用程序 ID

Android + AndroidAnnotations REST API