java.lang.NullPointerException 在 android.util.LruCache.put ( Android )
java.lang.NullPointerException in android.util.LruCache.put ( Android )
我在 google 我的应用 java.lang.NullPointerException android.util.LruCache.put[=15 中的 google 崩溃和 ANR 部分遇到了这个崩溃异常=]
我不知道出了什么问题我确实需要一些帮助,为什么我得到这个空指针异常以及如何修复它。
崩溃和 ANR:
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask.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.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.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);
}
这是因为 ThumbnailUtils.createVideoThumbnail() 可以 return null
因此,您需要在 addBitmapToMemoryCache() 方法中为位图添加检查 NPE
public static Bitmap createVideoThumbnail (String filePath, int kind)
为视频创建视频缩略图。可能 return null
如果视频损坏或格式不受支持。
所以,可能 bitmap
值为空。避免这种情况 像这样写你的代码:
if(bitmap != null)
{
addBitmapToMemoryCache(String.valueOf(params[0]), bitmap);
}
我在 google 我的应用 java.lang.NullPointerException android.util.LruCache.put[=15 中的 google 崩溃和 ANR 部分遇到了这个崩溃异常=]
我不知道出了什么问题我确实需要一些帮助,为什么我得到这个空指针异常以及如何修复它。
崩溃和 ANR:
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask.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.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.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);
}
这是因为 ThumbnailUtils.createVideoThumbnail() 可以 return null
因此,您需要在 addBitmapToMemoryCache() 方法中为位图添加检查 NPE
public static Bitmap createVideoThumbnail (String filePath, int kind)
为视频创建视频缩略图。可能 return null
如果视频损坏或格式不受支持。
所以,可能 bitmap
值为空。避免这种情况 像这样写你的代码:
if(bitmap != null)
{
addBitmapToMemoryCache(String.valueOf(params[0]), bitmap);
}