NullPointerException on PreferenceManager.getDefaultSharedPreferences on startup

So, I will redo my application A few months ago (fixing bugs and improving some of them). But now I have come to a problem that I have not found and do not know what it is. If I run the app on my phone it gives 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 someone point me in the direction where I need to see what is the error?

+3


source to share


1 answer


From the logarithm, it appears that you have called the method

 PreferenceManager.getDefaultSharedPreferences(context); 

      



If the context here is zero, then there is the possibility of such an exception. So try passing a valid context as a parameter. you can use the following method.

Context context = getApplicationContext();

      

+12


source







All Articles