![](/img/trans.png)
[英]Will GC collect an object referred to by a SoftReference and a WeakReference?
[英]SoftReference / WeakReference Nullpointer
在下一行接收NPE
if( mCache.containsKey(key) ){
在使用以下代码对文件图像进行解码时,尝试对哈希表使用SoftReference WeakReference。
private WeakHashMap<String, SoftReference<Bitmap>> mCache;
public Bitmap get(String key, BmpWrap image, BitmapFactory.Options options){
File root = mContext.getFilesDir();
String name = Activity.fullScreenActive ? image.name + ".png" : image.name
+ ".stat.png";
File img = new File(root, name);
if(key == null){
return null;
}
if( mCache.containsKey(key) ){
SoftReference<Bitmap> reference = mCache.get(key);
image.bmp = reference.get();
if( image.bmp != null ){
return image.bmp;
}
image.bmp = BitmapFactory.decodeFile(img.getAbsolutePath(), options);
mCache.put(key, new SoftReference<Bitmap>(image.bmp));
return image.bmp;
}
if( img.exists() ){
image.bmp = BitmapFactory.decodeFile(img.getAbsolutePath(), options);
mCache.put(key, new SoftReference<Bitmap>(image.bmp));
return image.bmp;
} else{
throw new RuntimeException("Boooom!");
}
}
private void scaleImage(BmpWrap image, BitmapFactory.Options options)
{
options.inTempStorage = new byte[16 * IMG_BUFFER_LEN];
options.inPreferredConfig = Bitmap.Config.RGB_565;
options.inDither = false;
try {
Field f = options.getClass().getField("inPurgeable");
f.set(options, Boolean.TRUE);
Field f2 = options.getClass().getField("inInputShareable");
f2.set(options, Boolean.TRUE);
} catch (Exception ignore) {
}
File root = mContext.getFilesDir();
String name = Activity.fullScreenActive ? image.name + ".png" : image.name
+ ".stat.png";
File img = new File(root, name);
// maybe it's jpeg
if (!img.exists()) {
img = new File(root, name.replace(".png", ".jpg"));
}
if (img.exists()) {
if (image.bmp == null || Activity.fullScreenSwitch) {
if (image.bmp != null)
image.bmp.recycle();
image.bmp = null;
**get(name, image, options);**
//image.bmp = BitmapFactory.decodeFile(img.getAbsolutePath(), options);
} else if (image.bmp == null) {
image.bmp = BitmapFactory.decodeResource(getResources(),
getResourceByString(image.name), options);
}
} else {
image.bmp = BitmapFactory.decodeResource(getResources(),
getResourceByString(image.name), options);
}
}
堆
12-16 18:30:42.724: E/AndroidRuntime(9955): FATAL EXCEPTION: main
12-16 18:30:42.724: E/AndroidRuntime(9955): java.lang.RuntimeException: Boooom!
12-16 18:30:42.724: E/AndroidRuntime(9955): at com.apk.View$Thread.get(View.java:259)
12-16 18:30:42.724: E/AndroidRuntime(9955): at com.apk.View$Thread.scaleImage(View.java:296)
12-16 18:30:42.724: E/AndroidRuntime(9955): at com.apk.View$Thread.resizeImages(View.java:326)
12-16 18:30:42.724: E/AndroidRuntime(9955): at com.apk.View$Thread.setSurfaceSize(View.java:515)
12-16 18:30:42.724: E/AndroidRuntime(9955): at com.apk.View.surfaceChanged(View.java:889)
我想这是( 这是针对NPE的 )
private WeakHashMap<String, SoftReference<Bitmap>> mCache;
就像是,
private WeakHashMap<String, SoftReference<Bitmap>> mCache = new WeakHashMap<String, SoftReference<Bitmap>>();
试试这个,让我知道会发生什么..
NullPointerException表示您尝试访问mCache
为null。 发生RuntimeException的原因是,内存中具有给定名称的文件不存在。
另外,您没有在问题中问这个,但这似乎是正确的话。 为什么需要使用WeakHashMap? 它用于允许GC收集其键不再在任何地方引用的条目。 您将字符串用作键,为什么要依赖于它们是否可达? 该机制应该让位图被垃圾收集,因此实际需要的是对SoftReferences的普通HashMap。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.