Public shared preferences causes app to crash

假如想象 提交于 2019-12-13 01:25:49

问题


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();

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

01-23 18:02:51.764 20873-20873/com.example.griffin.dinnerplannerapp E/AndroidRuntime: FATAL EXCEPTION: main 01-23 18:02:51.764 20873-20873/com.example.griffin.dinnerplannerapp E/AndroidRuntime: Process: com.example.griffin.dinnerplannerapp, PID: 20873 01-23 18:02:51.764 20873-20873/com.example.griffin.dinnerplannerapp E/AndroidRuntime: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.griffin.dinnerplannerapp/com.example.griffin.dinnerplannerapp.sundayActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference 01-23 18:02:51.764 20873-20873/com.example.griffin.dinnerplannerapp E/AndroidRuntime:
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2282) 01-23 18:02:51.764 20873-20873/com.example.griffin.dinnerplannerapp E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2439) 01-23 18:02:51.764 20873-20873/com.example.griffin.dinnerplannerapp E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:162) 01-23 18:02:51.764 20873-20873/com.example.griffin.dinnerplannerapp E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1348) 01-23 18:02:51.764 20873-20873/com.example.griffin.dinnerplannerapp E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102) 01-23 18:02:51.764 20873-20873/com.example.griffin.dinnerplannerapp E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135) 01-23 18:02:51.764 20873-20873/com.example.griffin.dinnerplannerapp E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5421) 01-23 18:02:51.764 20873-20873/com.example.griffin.dinnerplannerapp E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 01-23 18:02:51.764 20873-20873/com.example.griffin.dinnerplannerapp E/AndroidRuntime:
at java.lang.reflect.Method.invoke(Method.java:372) 01-23 18:02:51.764 20873-20873/com.example.griffin.dinnerplannerapp E/AndroidRuntime:
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:914) 01-23 18:02:51.764 20873-20873/com.example.griffin.dinnerplannerapp E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:707) 01-23 18:02:51.764 20873-20873/com.example.griffin.dinnerplannerapp E/AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference 01-23 18:02:51.764 20873-20873/com.example.griffin.dinnerplannerapp E/AndroidRuntime:
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:106) 01-23 18:02:51.764 20873-20873/com.example.griffin.dinnerplannerapp E/AndroidRuntime: at com.example.griffin.dinnerplannerapp.sundayActivity.(sundayActivity.java:61) 01-23 18:02:51.764 20873-20873/com.example.griffin.dinnerplannerapp E/AndroidRuntime: at java.lang.reflect.Constructor.newInstance(Native Method) 01-23 18:02:51.764 20873-20873/com.example.griffin.dinnerplannerapp E/AndroidRuntime: at java.lang.Class.newInstance(Class.java:1606) 01-23 18:02:51.764 20873-20873/com.example.griffin.dinnerplannerapp E/AndroidRuntime: at android.app.Instrumentation.newActivity(Instrumentation.java:1066) 01-23 18:02:51.764 20873-20873/com.example.griffin.dinnerplannerapp E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2272) 01-23 18:02:51.764 20873-20873/com.example.griffin.dinnerplannerapp E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2439)  01-23 18:02:51.764 20873-20873/com.example.griffin.dinnerplannerapp E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:162)  01-23 18:02:51.764 20873-20873/com.example.griffin.dinnerplannerapp E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1348)  01-23 18:02:51.764 20873-20873/com.example.griffin.dinnerplannerapp E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)  01-23 18:02:51.764 20873-20873/com.example.griffin.dinnerplannerapp E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)  01-23 18:02:51.764 20873-20873/com.example.griffin.dinnerplannerapp E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5421)  01-23 18:02:51.764 20873-20873/com.example.griffin.dinnerplannerapp E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)  01-23 18:02:51.764 20873-20873/com.example.griffin.dinnerplannerapp E/AndroidRuntime:
at java.lang.reflect.Method.invoke(Method.java:372)  01-23 18:02:51.764 20873-20873/com.example.griffin.dinnerplannerapp E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:914)  01-23 18:02:51.764 20873-20873/com.example.griffin.dinnerplannerapp E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:707)

 

Any help would be greatly appreciated thanks in advance.


回答1:


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

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.




回答2:


Have you declared permissions to Access external storage in you Manifest?

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Edit 1: Searched in the LogCat for a Detail found this

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()

take a look here Get application context returns null Maybe this helps.

Edit 2: I add the main parts of the link:

You should create a static Context instance in onCreate() and make sure that you declare the AndroidManifest.xml.

The Code for the context instance:

public class MyApp extends Application {

//private static MyApp instance; private static Context mContext;

public static MyApp getInstance() {
    return instance;
}

public static Context getContext() {
  //  return instance.getApplicationContext();
  return mContext;
}

@Override
public void onCreate() {
    super.onCreate();
//  instance = this;
 mContext = getApplicationContext();    
}

}

And the Manifest:

<application android:name="com.mypackage.MyApp"> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> ... </application>



来源:https://stackoverflow.com/questions/34970100/public-shared-preferences-causes-app-to-crash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!