0

此代码在我的真实设备上运行良好,但如果我在模拟器上运行我的应用程序,则会出现空指针异常-

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;

    }

我的日志猫:

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)

我尝试在我的android清单上设置外部存储但没有运气-

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

1 回答 1

0

如果 Emulator 没有指定缓存文件,则模拟器的默认行为是使用临时文件。

有关磁盘映像的更多信息,请使用 -help-disk-images。见http://developer.android.com/tools/help/emulator.html

于 2013-07-09T08:46:05.473 回答