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;
}

Logcat:

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 に答える