2011-12-16 81 views
0

在下面线SoftReference的/ WeakReference的空指针

if(mCache.containsKey(key)){ 

尝试使用SoftReference的WeakReference的同时用下面的代码decodingFile图像散列映射接听NPE。

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) 
+0

等等,你确定你想要一个SoftReference的WeakReference到一个位图吗? – kabuko 2011-12-16 07:18:24

+0

在某些情况下,我会在get方法调用下的注释行上接收OOM。所以希望实现这个来阻止这些崩溃。 – user1097075 2011-12-16 07:24:55

+0

当然,但是正常的WeakHashMap 有什么问题呢? – kabuko 2011-12-16 07:54:27

回答

0

我觉得这是,(这是NPE

private WeakHashMap<String, SoftReference<Bitmap>> mCache; 

喜欢的东西,

private WeakHashMap<String, SoftReference<Bitmap>> mCache = new WeakHashMap<String, SoftReference<Bitmap>>(); 

试试这个,让我知道发生了什么..

0

NullPointerException意味着mCache在您尝试访问它时为null。并且您的RuntimeException发生是因为具有给定名称的内存中的文件不存在。

此外,你没有问这个问题,但它似乎是一个正确的说法。为什么你需要使用WeakHashMap?它用于允许GC收集其密钥不再在任何地方被引用的条目。你使用字符串作为关键字,为什么你要依赖于它们是否可达?该机制应该让位图被垃圾回收,所以普通的HashMap到SoftReferences就是你实际需要的。