4

So I'm remaking my application of A couple of months ago(getting the faults out, and making some things better) But now I came up on to A problem that I did'nt find and don't know what it is. If I"m running the application on my phone it give this error

01-20 22:37:46.595: E/AndroidRuntime(7350): FATAL EXCEPTION: main
01-20 22:37:46.595: E/AndroidRuntime(7350): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.stevedc.thinklogic/com.stevedc.thinklogic.TowerHanoi}: java.lang.NullPointerException
01-20 22:37:46.595: E/AndroidRuntime(7350):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1891)
01-20 22:37:46.595: E/AndroidRuntime(7350):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
01-20 22:37:46.595: E/AndroidRuntime(7350):     at android.app.ActivityThread.access$600(ActivityThread.java:127)
01-20 22:37:46.595: E/AndroidRuntime(7350):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
01-20 22:37:46.595: E/AndroidRuntime(7350):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-20 22:37:46.595: E/AndroidRuntime(7350):     at android.os.Looper.loop(Looper.java:137)
01-20 22:37:46.595: E/AndroidRuntime(7350):     at android.app.ActivityThread.main(ActivityThread.java:4441)
01-20 22:37:46.595: E/AndroidRuntime(7350):     at java.lang.reflect.Method.invokeNative(Native Method)
01-20 22:37:46.595: E/AndroidRuntime(7350):     at java.lang.reflect.Method.invoke(Method.java:511)
01-20 22:37:46.595: E/AndroidRuntime(7350):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-20 22:37:46.595: E/AndroidRuntime(7350):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-20 22:37:46.595: E/AndroidRuntime(7350):     at dalvik.system.NativeStart.main(Native Method)
01-20 22:37:46.595: E/AndroidRuntime(7350): Caused by: java.lang.NullPointerException
01-20 22:37:46.595: E/AndroidRuntime(7350):     at android.preference.PreferenceManager.getDefaultSharedPreferencesName(PreferenceManager.java:371)
01-20 22:37:46.595: E/AndroidRuntime(7350):     at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:366)
01-20 22:37:46.595: E/AndroidRuntime(7350):     at com.stevedc.thinklogic.TowerHanoi.<init>(TowerHanoi.java:42)
01-20 22:37:46.595: E/AndroidRuntime(7350):     at java.lang.Class.newInstanceImpl(Native Method)
01-20 22:37:46.595: E/AndroidRuntime(7350):     at java.lang.Class.newInstance(Class.java:1319)
01-20 22:37:46.595: E/AndroidRuntime(7350):     at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
01-20 22:37:46.595: E/AndroidRuntime(7350):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1882)

can somebody point me in the direction where I need to see wich the fault is?

stevedc
  • 483
  • 3
  • 8
  • 26
  • 1
    I isolate the cause and put it in the title; note where the initial exception propagates from. (Your code *calls* PreferenceManager which then throws the Exception; considering create a *minimal* test-case that reproduces the behavior and post that code: also, consider moving the code to `OnCreate`; I am not sure if the Constructor () can access the SharedPreferences ..) –  Jan 20 '13 at 21:58

1 Answers1

13

From the logcat it seems that the method you called is

 PreferenceManager.getDefaultSharedPreferences(context); 

If the context here is null then there is a possibility of such exception. So try to pass a valid context as parameter. you can use the following method.

Context context = getApplicationContext();
stinepike
  • 54,068
  • 14
  • 92
  • 112
  • This is a little old, but @StinePike whats the difference between using activityContext vs applicationContext? – John Shelley Jun 25 '14 at 01:41
  • @JohnShelley .. They are both instances of Context, but the application instance is tied to the lifecycle of the application, while the Activity instance is tied to the lifecycle of an Activity. Thus, they have access to different information about the application environment... see this answer .. http://stackoverflow.com/a/4128799/931982 – stinepike Jun 25 '14 at 02:21
  • Is it always okay to use application for SharedPreferences then? I would assume so since they are used all around the application right? @StinePike – John Shelley Jun 25 '14 at 02:24
  • I mainly used Activity context when possible .. this answer was actually illustrate the initialization of context , instead of differentiating between activity and application context. – stinepike Jun 25 '14 at 03:55