示例来源: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
答案 0 :(得分:1)
您是否尝试在模拟器中运行它?
我的问题是我使用Galaxy Nexus和Nexus S进行了AVD配置,但没有为它们设置SD存储。您看到这些设备没有可以删除的SD卡,它们只有内部存储器,但是这个内存分为应用程序的“内部”分区和可以安装在PC上的“外部”分区。
Bitmapfun ImageCahcer类中的代码将调用方法Environment.isExternalStorageRemovable(),这将为提及的设备返回false,但由于您未指定SD卡大小,因此不会有外部存储。
解决方案是将SD卡大小设置为某个值,代码将起作用。
答案 1 :(得分: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;
}