-3

My android application crashes every time I run/launch it on both the emulator and a real device and returns this error message: Unfortunately "application_name" has stopped! This is what my logcat looks like:

08-05 22:34:24.452: D/dalvikvm(541): Not late-enabling CheckJNI (already on)
08-05 22:34:25.012: D/AndroidRuntime(541): Shutting down VM
08-05 22:34:25.012: W/dalvikvm(541): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
08-05 22:34:25.012: E/AndroidRuntime(541): FATAL EXCEPTION: main
08-05 22:34:25.012: E/AndroidRuntime(541): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.tutorial.sharedpreferences/com.tutorial.sharedpreferences.Main}: java.lang.NullPointerException
08-05 22:34:25.012: E/AndroidRuntime(541):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1879)
08-05 22:34:25.012: E/AndroidRuntime(541):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
08-05 22:34:25.012: E/AndroidRuntime(541):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
08-05 22:34:25.012: E/AndroidRuntime(541):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
08-05 22:34:25.012: E/AndroidRuntime(541):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-05 22:34:25.012: E/AndroidRuntime(541):  at android.os.Looper.loop(Looper.java:137)
08-05 22:34:25.012: E/AndroidRuntime(541):  at android.app.ActivityThread.main(ActivityThread.java:4340)
08-05 22:34:25.012: E/AndroidRuntime(541):  at java.lang.reflect.Method.invokeNative(Native Method)
08-05 22:34:25.012: E/AndroidRuntime(541):  at java.lang.reflect.Method.invoke(Method.java:511)
08-05 22:34:25.012: E/AndroidRuntime(541):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-05 22:34:25.012: E/AndroidRuntime(541):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-05 22:34:25.012: E/AndroidRuntime(541):  at dalvik.system.NativeStart.main(Native Method)
08-05 22:34:25.012: E/AndroidRuntime(541): Caused by: java.lang.NullPointerException
08-05 22:34:25.012: E/AndroidRuntime(541):  at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:153)
08-05 22:34:25.012: E/AndroidRuntime(541):  at com.tutorial.sharedpreferences.Main.<init>(Main.java:13)
08-05 22:34:25.012: E/AndroidRuntime(541):  at java.lang.Class.newInstanceImpl(Native Method)
08-05 22:34:25.012: E/AndroidRuntime(541):  at java.lang.Class.newInstance(Class.java:1319)
08-05 22:34:25.012: E/AndroidRuntime(541):  at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
08-05 22:34:25.012: E/AndroidRuntime(541):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1870)
08-05 22:34:25.012: E/AndroidRuntime(541):  ... 11 more
030
  • 10,842
  • 12
  • 78
  • 123
  • 2
    possible duplicate of [Unfortunately MyApp has stopped. How can I solve this?](http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – panini Aug 05 '14 at 22:06

1 Answers1

1

You are attempting to call getSharedPreferences() from an initializer. You cannot safely call methods you inherit from Activity until after super.onCreate() is called.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491