5

示例来源:http: //developer.android.com/training/displaying-bitmaps/index.html

不知道为什么我根本无法运行该项目。

原木猫

09-06 13:14:13.664: E/AndroidRuntime(591): FATAL EXCEPTION: main
09-06 13:14:13.664: E/AndroidRuntime(591): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.bitmapfun/com.example.android.bitmapfun.ui.ImageGridActivity}: java.lang.NullPointerException
09-06 13:14:13.664: E/AndroidRuntime(591):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
09-06 13:14:13.664: E/AndroidRuntime(591):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
09-06 13:14:13.664: E/AndroidRuntime(591):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
09-06 13:14:13.664: E/AndroidRuntime(591):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
09-06 13:14:13.664: E/AndroidRuntime(591):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-06 13:14:13.664: E/AndroidRuntime(591):  at android.os.Looper.loop(Looper.java:137)
09-06 13:14:13.664: E/AndroidRuntime(591):  at android.app.ActivityThread.main(ActivityThread.java:4340)
09-06 13:14:13.664: E/AndroidRuntime(591):  at java.lang.reflect.Method.invokeNative(Native Method)
09-06 13:14:13.664: E/AndroidRuntime(591):  at java.lang.reflect.Method.invoke(Method.java:511)
09-06 13:14:13.664: E/AndroidRuntime(591):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-06 13:14:13.664: E/AndroidRuntime(591):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-06 13:14:13.664: E/AndroidRuntime(591):  at dalvik.system.NativeStart.main(Native Method)
09-06 13:14:13.664: E/AndroidRuntime(591): Caused by: java.lang.NullPointerException
09-06 13:14:13.664: E/AndroidRuntime(591):  at com.example.android.bitmapfun.util.DiskLruCache.getDiskCacheDir(DiskLruCache.java:268)
09-06 13:14:13.664: E/AndroidRuntime(591):  at com.example.android.bitmapfun.util.ImageCache.init(ImageCache.java:121)
09-06 13:14:13.664: E/AndroidRuntime(591):  at com.example.android.bitmapfun.util.ImageCache.<init>(ImageCache.java:61)
09-06 13:14:13.664: E/AndroidRuntime(591):  at com.example.android.bitmapfun.util.ImageCache.findOrCreateCache(ImageCache.java:107)
09-06 13:14:13.664: E/AndroidRuntime(591):  at com.example.android.bitmapfun.ui.ImageGridFragment.onCreate(ImageGridFragment.java:96)
09-06 13:14:13.664: E/AndroidRuntime(591):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:834)
09-06 13:14:13.664: E/AndroidRuntime(591):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
09-06 13:14:13.664: E/AndroidRuntime(591):  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:622)
09-06 13:14:13.664: E/AndroidRuntime(591):  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416)
09-06 13:14:13.664: E/AndroidRuntime(591):  at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:505)
09-06 13:14:13.664: E/AndroidRuntime(591):  at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1133)
09-06 13:14:13.664: E/AndroidRuntime(591):  at android.app.Activity.performStart(Activity.java:4475)
09-06 13:14:13.664: E/AndroidRuntime(591):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1928)
09-06 13:14:13.664: E/AndroidRuntime(591):  ... 11 more
4

2 回答 2

1

您是否尝试在模拟器中运行它?

我的问题是我对 Galaxy Nexus 和 Nexus S 进行了 AVD 配置,但没有为它们设置 SD 存储。您会看到这些设备没有可以删除的 SD 卡,它们只有内部存储器,但此存储器分为应用程序的“内部”分区和可以安装在 PC 上的“外部”分区。

Bitmapfun ImageCahcer 类中的代码将调用 Environment.isExternalStorageRemovable() 方法,这将为提到的设备返回 false,但由于您没有指定 SD 卡大小,因此不会有外部存储。

解决方案是将 SD 卡大小设置为某个值,代码将起作用。

于 2013-01-28T16:40:23.483 回答
1

可能您将 null 作为键传递。您可以通过以下更改检查您的代码:

public static String hashKeyForDisk(String key) {
    String cacheKey;
    try {
        final MessageDigest mDigest = MessageDigest.getInstance("MD5");
        if (key == null) {
            Log.e("TEST","key = null");
            throw new IllegalArgumentException(" key == null");
        }

        if (mDigest == null) {
            Log.e("TEST","mDigest = null");

            return String.valueOf(key.hashCode());
        }
        byte[] bytes = key.getBytes();
        mDigest.update(bytes);
        cacheKey = bytesToHexString(mDigest.digest());
    } catch (NoSuchAlgorithmException e) {
        cacheKey = String.valueOf(key.hashCode());
    }
    return cacheKey;
}
于 2013-10-16T09:12:09.823 回答