0

由于某种未知原因,我的应用程序在启动时立即崩溃。如果我一开始就删除了 SharedPreferences..... 部分,那就没关系了,所以问题一定是关于 SharedPreferences 的!?当我第一次使用这个时,如果之前没有文件是不是创建的?

有什么问题?

String filename = "taggedImages";
SharedPreferences tagInfo = getSharedPreferences(filename, 0);



public void tagImage(String imagePath, String contactId){
    //Toast.makeText(Activity_1.this, "" + imagePath + " " + contactId, Toast.LENGTH_LONG).show();

    // Write to file
    SharedPreferences.Editor editor = tagInfo.edit();
    editor.putString(imagePath, contactId);
    editor.commit();

    // Disable the menu item "Tagged in this image"
    showMenuItem1 = false;
}

日志猫:

02-27 13:37:48.156: D/AndroidRuntime(27311): Shutting down VM
02-27 13:37:48.156: W/dalvikvm(27311): threadid=1: thread exiting with uncaught exception (group=0x40018578)
02-27 13:37:48.164: E/AndroidRuntime(27311): FATAL EXCEPTION: main
02-27 13:37:48.164: E/AndroidRuntime(27311): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{se.com.test.assignment.two/se.com.test.assignment.two.Activity_1}: java.lang.NullPointerException
02-27 13:37:48.164: E/AndroidRuntime(27311):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573)
02-27 13:37:48.164: E/AndroidRuntime(27311):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
02-27 13:37:48.164: E/AndroidRuntime(27311):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-27 13:37:48.164: E/AndroidRuntime(27311):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
02-27 13:37:48.164: E/AndroidRuntime(27311):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-27 13:37:48.164: E/AndroidRuntime(27311):    at android.os.Looper.loop(Looper.java:130)
02-27 13:37:48.164: E/AndroidRuntime(27311):    at android.app.ActivityThread.main(ActivityThread.java:3687)
02-27 13:37:48.164: E/AndroidRuntime(27311):    at java.lang.reflect.Method.invokeNative(Native Method)
02-27 13:37:48.164: E/AndroidRuntime(27311):    at java.lang.reflect.Method.invoke(Method.java:507)
02-27 13:37:48.164: E/AndroidRuntime(27311):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
02-27 13:37:48.164: E/AndroidRuntime(27311):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
02-27 13:37:48.164: E/AndroidRuntime(27311):    at dalvik.system.NativeStart.main(Native Method)
02-27 13:37:48.164: E/AndroidRuntime(27311): Caused by: java.lang.NullPointerException
02-27 13:37:48.164: E/AndroidRuntime(27311):    at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:146)
02-27 13:37:48.164: E/AndroidRuntime(27311):    at se.com.test.assignment.two.Activity_1.<init>(Activity_1.java:27)
02-27 13:37:48.164: E/AndroidRuntime(27311):    at java.lang.Class.newInstanceImpl(Native Method)
02-27 13:37:48.164: E/AndroidRuntime(27311):    at java.lang.Class.newInstance(Class.java:1409)
02-27 13:37:48.164: E/AndroidRuntime(27311):    at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
02-27 13:37:48.164: E/AndroidRuntime(27311):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565)
02-27 13:37:48.164: E/AndroidRuntime(27311):    ... 11 more
02-27 13:40:19.492: I/ApplicationPackageManager(27449): cscCountry is not German : NEE
02-27 13:40:21.679: I/ApplicationPackageManager(27449): cscCountry is not German : NEE
02-27 13:40:21.984: D/dalvikvm(27449): GC_EXTERNAL_ALLOC freed 64K, 48% free 2843K/5379K, external 514K/517K, paused 40ms
02-27 13:40:22.125: D/dalvikvm(27449): GC_EXTERNAL_ALLOC freed 166K, 48% free 2846K/5447K, external 1100K/1144K, paused 38ms
02-27 13:46:45.820: D/AndroidRuntime(27748): Shutting down VM
02-27 13:46:45.820: W/dalvikvm(27748): threadid=1: thread exiting with uncaught exception (group=0x40018578)
02-27 13:46:45.843: E/AndroidRuntime(27748): FATAL EXCEPTION: main
02-27 13:46:45.843: E/AndroidRuntime(27748): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{se.com.test.assignment.two/se.com.test.assignment.two.Activity_1}: java.lang.NullPointerException
02-27 13:46:45.843: E/AndroidRuntime(27748):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573)
02-27 13:46:45.843: E/AndroidRuntime(27748):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
02-27 13:46:45.843: E/AndroidRuntime(27748):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-27 13:46:45.843: E/AndroidRuntime(27748):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
02-27 13:46:45.843: E/AndroidRuntime(27748):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-27 13:46:45.843: E/AndroidRuntime(27748):    at android.os.Looper.loop(Looper.java:130)
02-27 13:46:45.843: E/AndroidRuntime(27748):    at android.app.ActivityThread.main(ActivityThread.java:3687)
02-27 13:46:45.843: E/AndroidRuntime(27748):    at java.lang.reflect.Method.invokeNative(Native Method)
02-27 13:46:45.843: E/AndroidRuntime(27748):    at java.lang.reflect.Method.invoke(Method.java:507)
02-27 13:46:45.843: E/AndroidRuntime(27748):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
02-27 13:46:45.843: E/AndroidRuntime(27748):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
02-27 13:46:45.843: E/AndroidRuntime(27748):    at dalvik.system.NativeStart.main(Native Method)
02-27 13:46:45.843: E/AndroidRuntime(27748): Caused by: java.lang.NullPointerException
02-27 13:46:45.843: E/AndroidRuntime(27748):    at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:146)
02-27 13:46:45.843: E/AndroidRuntime(27748):    at se.com.test.assignment.two.Activity_1.<init>(Activity_1.java:27)
02-27 13:46:45.843: E/AndroidRuntime(27748):    at java.lang.Class.newInstanceImpl(Native Method)
02-27 13:46:45.843: E/AndroidRuntime(27748):    at java.lang.Class.newInstance(Class.java:1409)
02-27 13:46:45.843: E/AndroidRuntime(27748):    at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
02-27 13:46:45.843: E/AndroidRuntime(27748):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565)
02-27 13:46:45.843: E/AndroidRuntime(27748):    ... 11 more
4

1 回答 1

2

我假设你在打电话

SharedPreferences tagInfo = getSharedPreferences(filename, 0);

太早了。在你的类中声明 tagInfo 并在onCreate 你的活动中初始化它:

SharedPreferences tagInfo; //declaration in your class
...
@Override
onCreate(...){
  ...
  tagInfo = getSharedPreferences(filename, 0);
}
于 2013-02-27T12:52:34.993 回答