0

此代碼DiskLruCache空指針異常工作正常我真正的設備上,但讓空指針異常,如果我跑我的emulator-在模擬器

應用DiskLruImageCache.java

public DiskLruImageCache(Context context, String uniqueName, 
      int diskCacheSize, CompressFormat compressFormat, int quality) { 
     try { 
      final File diskCacheDir = getDiskCacheDir(context, uniqueName); 
      mDiskCache = DiskLruCache.open(diskCacheDir, APP_VERSION, 
        VALUE_COUNT, diskCacheSize); 
      mCompressFormat = compressFormat; 
      mCompressQuality = quality; 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

public void put(String key, Bitmap data) { 

     DiskLruCache.Editor editor = null; 
     try { 
      editor = mDiskCache.edit(key); 
      // showMsg("Editor = "+ editor); 
      if (editor == null) { 
       return; 
      } 

      if (writeBitmapToFile(data, editor)) { 
       mDiskCache.flush(); 
       editor.commit(); 
       if (BuildConfig.DEBUG) { 
       } 
      } else { 
       editor.abort(); 
       if (BuildConfig.DEBUG) { 
       } 
      } 
     } catch (IOException e) { 
      if (BuildConfig.DEBUG) { 
      } 
      try { 
       if (editor != null) { 
        editor.abort(); 
       } 
      } catch (IOException ignored) { 
      } 
     } 

    } 

public Bitmap getBitmap(String key) { 

     Bitmap bitmap = null; 
     DiskLruCache.Snapshot snapshot = null; 
     try { 

      snapshot = mDiskCache.get(key); 
      if (snapshot == null) { 
       return null; 
      } 
      final InputStream in = snapshot.getInputStream(0); 
      if (in != null) { 
       final BufferedInputStream buffIn = new BufferedInputStream(in); 
       bitmap = BitmapFactory.decodeStream(buffIn).copy(
         Config.ARGB_4444, true); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      if (snapshot != null) { 
       snapshot.close(); 
      } 
     } 

     if (BuildConfig.DEBUG) { 

     } 

     return bitmap; 

    } 

我的logcat :

07-09 08:29:45.877: E/AndroidRuntime(2266): FATAL EXCEPTION: main 
07-09 08:29:45.877: E/AndroidRuntime(2266): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.apper.main/com.apper.main.MyFragmentActivity}: java.lang.NullPointerException 
07-09 08:29:45.877: E/AndroidRuntime(2266):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
07-09 08:29:45.877: E/AndroidRuntime(2266):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
07-09 08:29:45.877: E/AndroidRuntime(2266):  at android.app.ActivityThread.access$600(ActivityThread.java:141) 
07-09 08:29:45.877: E/AndroidRuntime(2266):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
07-09 08:29:45.877: E/AndroidRuntime(2266):  at android.os.Handler.dispatchMessage(Handler.java:99) 
07-09 08:29:45.877: E/AndroidRuntime(2266):  at android.os.Looper.loop(Looper.java:137) 
07-09 08:29:45.877: E/AndroidRuntime(2266):  at android.app.ActivityThread.main(ActivityThread.java:5041) 
07-09 08:29:45.877: E/AndroidRuntime(2266):  at java.lang.reflect.Method.invokeNative(Native Method) 
07-09 08:29:45.877: E/AndroidRuntime(2266):  at java.lang.reflect.Method.invoke(Method.java:511) 
07-09 08:29:45.877: E/AndroidRuntime(2266):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
07-09 08:29:45.877: E/AndroidRuntime(2266):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
07-09 08:29:45.877: E/AndroidRuntime(2266):  at dalvik.system.NativeStart.main(Native Method) 
07-09 08:29:45.877: E/AndroidRuntime(2266): Caused by: java.lang.NullPointerException 
07-09 08:29:45.877: E/AndroidRuntime(2266):  at com.apper.util.DiskLruImageCache.getBitmap(DiskLruImageCache.java:109) 

我試着在m上設置外部存儲ŸAndroid清單,但沒有luck-

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
+0

「DiskLruImageCache」中第109行的內容是什麼? – Raghunandan

+0

'snapshot = mDiskCache.get(key);'。模擬器上的mDiskCache爲null,但實際設備並非如此。 –

回答