Application crash at start with Shared Preferences
<p>For some unknown reason my application crash straight away at start. If I remove the SharedPreferences..... part in the beginning, then it's OK, so the problem has to be about the SharedPreferences!? When I use this for the first time, isn't the file created if there is none before?</p>

<p>What could be wrong? </p>

<pre><code>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;
}
</code></pre>

<p>Logcat:</p>

<pre><code>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.&lt;init&gt;(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.&lt;init&gt;(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
</code></pre>
