android - 获取图像缓存时出现 NullPointerException

标签 android caching nullpointerexception

我正在使用来自 Android's BitmapFun sample code 的类并且在某些设备上遇到了问题(我的应用程序在我测试过的所有设备上都没有崩溃,但在其他人身上)。

注意:发生这种情况的Android版本是:2.3.3-2.3.7、4.0.3-4.0.4、4.1和4.2

在我的 Google 开发者控制台中,我得到以下堆栈跟踪:

java.lang.RuntimeException: Unable to start activity 
ComponentInfo{com.question/com.question.ui.question}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2077)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
at android.app.ActivityThread.access$600(ActivityThread.java:134)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:4624)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.question.util.ImageCache.getDiskCacheDir(ImageCache.java:568)
at com.question.util.ImageCache$ImageCacheParams.<init>(ImageCache.java:488)
at com.question.ui.question.onCreate(question.java:58)
at android.app.Activity.performCreate(Activity.java:4479)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2041)
... 11 more

我调查了这个并找到了一个 question with the same problem .我根据该答案进行了一些调整,但同样的问题仍然存在。

导致问题的代码:

public static File getDiskCacheDir(Context context, String uniqueName) {
    // Check if media is mounted or storage is built-in, if so, try and use external cache dir
    // otherwise use internal cache dir
    final String cachePath =
            Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) ||
            !isExternalStorageRemovable() ? getExternalCacheDir(context).getPath() :
                            context.getCacheDir().getPath();

    return new File(cachePath + File.separator + uniqueName);
}

如果有人知道这里的问题是什么并且可以解释,那将会有所帮助!

最佳答案

我遇到了这个问题,发现我的 AVD 模拟器配置不正确。具体来说,空指针是由于没有定义和分配给Virtual Device的存储空间。

要解决此问题,请创建一个新的或使用 SD 卡存储编辑您的 Android 虚拟设备。在 Eclipse 中:选择 Andoid Virtual Device Manager ICON=> Create New=>...页面底部附近有一个名为 SD Card 的部分:Enter a value for SD Card size。

这里是 a link描述了如何使用适当的存储配置 AVD 模拟器。

这里是 a link这提供了一个很好的技术定义,为什么会生成 NullPointerException。

关于android - 获取图像缓存时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22942126/

相关文章:

javascript - 缓存:myscript-1125.js vs myscript.js?1125

java - 未抛出 Spring 的自定义异常

android - 无法使用 "adb reverse"连接到开发服务器

iphone - iPhone 上有可用的磁盘缓存吗?

c# - System.Runtime.Caching.MemoryCache 是否在同一服务器中跨进程共享

java - 使用java注释生成Postgresql表

android - 更改时间强制关闭 : Time Picker (NPE)

android - 使用glOrthof的OpenglES多平台屏幕比例解决方案

android - setFirestoreSettings 问题

Android:将功能推送到应用程序中