本文介绍了公共共享首选项导致应用崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个共享首选项,可以通过我的类中的多种方法访问.为此,我将共享首选项设置为 public:

I need to have a shared preference that can be accessed by multiple methods in my class. To do this I set the shared preference public like this:

public SharedPreferences myPreference = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

然后我可以通过两种不同的方法像这样访问它:

Then I can access it like this from two different methods:

SharedPreferences.Editor editor=myPreference.edit();

当我这样做时,应用程序崩溃,这是 logcat:

when I do this the app crashes, here is the logcat:

01-23 18:02:51.764 20873-20873/com.example.griffin.dinnerplannerappE/AndroidRuntime:致命异常:主要 01-23 18:02:51.76420873-20873/com.example.griffin.dinnerplannerapp E/AndroidRuntime:进程:com.example.griffin.dinnerplannerapp,PID:20873 01-2318:02:51.764 20873-20873/com.example.griffin.dinnerplannerappE/AndroidRuntime:java.lang.RuntimeException:无法实例化活动组件信息{com.example.griffin.dinnerplannerapp/com.example.griffin.dinnerplannerapp.sundayActivity}:java.lang.NullPointerException:尝试调用虚方法'android.content.Contextandroid.content.Context.getApplicationContext()' 在一个空对象上参考 01-23 18:02:51.76420873-20873/com.example.griffin.dinnerplannerapp E/AndroidRuntime:
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2282)01-23 18:02:51.764 20873-20873/com.example.griffin.dinnerplannerappE/Android 运行时:在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2439)01-23 18:02:51.764 20873-20873/com.example.griffin.dinnerplannerappE/AndroidRuntime:在android.app.ActivityThread.access$800(ActivityThread.java:162) 01-2318:02:51.764 20873-20873/com.example.griffin.dinnerplannerappE/AndroidRuntime:在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1348)01-23 18:02:51.764 20873-20873/com.example.griffin.dinnerplannerappE/AndroidRuntime:在android.os.Handler.dispatchMessage(Handler.java:102) 01-2318:02:51.764 20873-20873/com.example.griffin.dinnerplannerappE/AndroidRuntime:在 android.os.Looper.loop(Looper.java:135) 01-2318:02:51.764 20873-20873/com.example.griffin.dinnerplannerappE/AndroidRuntime:在android.app.ActivityThread.main(ActivityThread.java:5421) 01-2318:02:51.764 20873-20873/com.example.griffin.dinnerplannerappE/AndroidRuntime:在 java.lang.reflect.Method.invoke(Native方法) 01-23 18:02:51.76420873-20873/com.example.griffin.dinnerplannerapp E/AndroidRuntime:
在 java.lang.reflect.Method.invoke(Method.java:372) 01-23 18:02:51.76420873-20873/com.example.griffin.dinnerplannerapp E/AndroidRuntime:
在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:914)01-23 18:02:51.764 20873-20873/com.example.griffin.dinnerplannerappE/AndroidRuntime:在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:707) 01-2318:02:51.764 20873-20873/com.example.griffin.dinnerplannerappE/AndroidRuntime: 引起: java.lang.NullPointerException: Attempt调用虚拟方法 'android.content.Contextandroid.content.Context.getApplicationContext()' 在一个空对象上参考 01-23 18:02:51.76420873-20873/com.example.griffin.dinnerplannerapp E/AndroidRuntime:
在android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:106)01-23 18:02:51.764 20873-20873/com.example.griffin.dinnerplannerappE/AndroidRuntime:在com.example.griffin.dinnerplannerapp.sundayActivity.(sundayActivity.java:61)01-23 18:02:51.764 20873-20873/com.example.griffin.dinnerplannerappE/AndroidRuntime:在java.lang.reflect.Constructor.newInstance(Native Method) 01-2318:02:51.764 20873-20873/com.example.griffin.dinnerplannerappE/AndroidRuntime:在 java.lang.Class.newInstance(Class.java:1606)01-23 18:02:51.764 20873-20873/com.example.griffin.dinnerplannerappE/AndroidRuntime:在android.app.Instrumentation.newActivity(Instrumentation.java:1066)01-23 18:02:51.764 20873-20873/com.example.griffin.dinnerplannerappE/AndroidRuntime:在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2272)01-23 18:02:51.764 20873-20873/com.example.griffin.dinnerplannerappE/AndroidRuntime:在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2439)01-23 18:02:51.764 20873-20873/com.example.griffin.dinnerplannerappE/AndroidRuntime:在android.app.ActivityThread.access$800(ActivityThread.java:162) 01-2318:02:51.764 20873-20873/com.example.griffin.dinnerplannerappE/AndroidRuntime:在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1348)01-23 18:02:51.764 20873-20873/com.example.griffin.dinnerplannerappE/AndroidRuntime:在android.os.Handler.dispatchMessage(Handler.java:102) 01-2318:02:51.764 20873-20873/com.example.griffin.dinnerplannerappE/AndroidRuntime:在 android.os.Looper.loop(Looper.java:135)01-23 18:02:51.764 20873-20873/com.example.griffin.dinnerplannerappE/AndroidRuntime:在android.app.ActivityThread.main(ActivityThread.java:5421) 01-2318:02:51.764 20873-20873/com.example.griffin.dinnerplannerappE/AndroidRuntime:在 java.lang.reflect.Method.invoke(Native方法)01-23 18:02:51.76420873-20873/com.example.griffin.dinnerplannerapp E/AndroidRuntime:
在 java.lang.reflect.Method.invoke(Method.java:372) 01-2318:02:51.764 20873-20873/com.example.griffin.dinnerplannerappE/AndroidRuntime:在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:914)01-23 18:02:51.764 20873-20873/com.example.griffin.dinnerplannerappE/AndroidRuntime:在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:707)

在此先感谢您的任何帮助.

Any help would be greatly appreciated thanks in advance.

推荐答案

在调用 onCreate() 方法之前不能调用 getApplicationContext(),这可能这就是为什么它返回 null 并给你这个错误.

You can't call getApplicationContext() before the onCreate() method is called, that may be why it's returning null and giving you this error.

因此,我建议您在 onCreate() 中设置该字段,或者更好地直接使用 PreferenceManager.getDefaultSharedPreferences(getApplicationContext())SharedPreferences.

Thus, I suggest you set the field in the onCreate(), or better yet just use PreferenceManager.getDefaultSharedPreferences(getApplicationContext()) directly wherever you need to use the SharedPreferences.

这篇关于公共共享首选项导致应用崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-08 22:57