所以我在几个月前重新制作我的应用程序(找出错误,并使一些事情变得更好)但现在我想到了一个我没有找到并且不知道它是什么的问题。如果我在手机上运行该应用程序,则会出现此错误

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)

有人可以指出我需要查看的错误所在的方向吗?

最佳答案

从 logcat 看来,您调用的方法是

 PreferenceManager.getDefaultSharedPreferences(context);

如果此处的上下文为空,则可能会出现此类异常。所以尝试传递一个有效的上下文作为参数。您可以使用以下方法。
Context context = getApplicationContext();

关于android - 启动时 PreferenceManager.getDefaultSharedPreferences 处的 NullPointerException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14429754/

10-11 22:35