如何在应用程序中获取SharedPreferences?
How to getSharedPreferences in Application?
我试图在应用程序中调用 getSharedPreferences
...
public class App extends Application{
public App() {
SharedPreferences prefs = getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
}
}
...但是我得到了 NullPointerException
:
java.lang.NullPointerException: Attempt to invoke virtual method
'android.content.SharedPreferences
android.content.Context.getSharedPreferences(java.lang.String, int)'
on a null object reference
我也试过这个并得到同样的异常:
Context con = getApplicationContext();
如何调用 getSharedPreferences
?
在 onCreate()
方法中获取 SharedPreferences 对象,而不是在构造函数中
在您的应用中覆盖 onCreate()
,
@Override
public void onCreate() {
super.onCreate();
然后在那里做。不要忘记在 AndroidManifest
中声明您的 Application
子类。例如
<application
android:name="App"
您得到了一个空指针,因为您是从构造函数中调用它的。此时应用程序上下文尚未创建。
尝试从 Application class.
的 onCreate() 方法调用它
Application
是 Android class 并且有自己的生命周期。您不能在那边启动或执行与 SharedPreference
相关的任何其他操作。因为它需要 Context
而 Application
的 Context
尚未使用它自己的生命周期进行初始化。因此,最佳做法是在 onCreate
方法中启动任何内容。
还有一个建议,让你的Application
class单身。
您不能在 Application 的构造函数中创建共享首选项对象,而是在应用程序的 onCreate 方法中创建它 class。这是代码块
public class App extends Application{
public App() {
}
@Override
public void onCreate(){
SharedPreferences prefs = getApplicationContext().getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
}
}
我想至少你可以这样试试;
public class MyApplicationclass extends Application {
Context context;
public MyApplicationclass() {
}
public MyApplicationclass(Context context) {
this.context = context;
}
}
现在你有了上下文;
所以很容易创建和使用 sharedpreferences
public class Preferences {
private static Preferences preferences = null;
private SharedPreferences.Editor editor;
private String userName;
private String token;
private String password;
private Preferences(Context context) {
editor = getCurrent(context).edit();
}
public static synchronized Preferences getInstance(Context context) {
if (preferences == null) {
preferences = new Preferences(context);
}
return preferences;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getDefaults(String key, Context context) {
return getCurrent(context).getString(key, null);
}
public void store(String key, String value) {
editor.putString(key, value);
editor.apply();
}
public void remove(String key) {
editor.remove(key);
editor.apply();
}
private SharedPreferences getCurrent(Context context) {
return context.getSharedPreferences("app_preference_key"), Context.MODE_PRIVATE);
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
这是首选项的单例实现。
我试图在应用程序中调用 getSharedPreferences
...
public class App extends Application{
public App() {
SharedPreferences prefs = getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
}
}
...但是我得到了 NullPointerException
:
java.lang.NullPointerException: Attempt to invoke virtual method
'android.content.SharedPreferences
android.content.Context.getSharedPreferences(java.lang.String, int)'
on a null object reference
我也试过这个并得到同样的异常:
Context con = getApplicationContext();
如何调用 getSharedPreferences
?
在 onCreate()
方法中获取 SharedPreferences 对象,而不是在构造函数中
在您的应用中覆盖 onCreate()
,
@Override
public void onCreate() {
super.onCreate();
然后在那里做。不要忘记在 AndroidManifest
中声明您的 Application
子类。例如
<application
android:name="App"
您得到了一个空指针,因为您是从构造函数中调用它的。此时应用程序上下文尚未创建。 尝试从 Application class.
的 onCreate() 方法调用它Application
是 Android class 并且有自己的生命周期。您不能在那边启动或执行与 SharedPreference
相关的任何其他操作。因为它需要 Context
而 Application
的 Context
尚未使用它自己的生命周期进行初始化。因此,最佳做法是在 onCreate
方法中启动任何内容。
还有一个建议,让你的Application
class单身。
您不能在 Application 的构造函数中创建共享首选项对象,而是在应用程序的 onCreate 方法中创建它 class。这是代码块
public class App extends Application{
public App() {
}
@Override
public void onCreate(){
SharedPreferences prefs = getApplicationContext().getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
}
}
我想至少你可以这样试试;
public class MyApplicationclass extends Application {
Context context;
public MyApplicationclass() {
}
public MyApplicationclass(Context context) {
this.context = context;
}
}
现在你有了上下文; 所以很容易创建和使用 sharedpreferences
public class Preferences {
private static Preferences preferences = null;
private SharedPreferences.Editor editor;
private String userName;
private String token;
private String password;
private Preferences(Context context) {
editor = getCurrent(context).edit();
}
public static synchronized Preferences getInstance(Context context) {
if (preferences == null) {
preferences = new Preferences(context);
}
return preferences;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getDefaults(String key, Context context) {
return getCurrent(context).getString(key, null);
}
public void store(String key, String value) {
editor.putString(key, value);
editor.apply();
}
public void remove(String key) {
editor.remove(key);
editor.apply();
}
private SharedPreferences getCurrent(Context context) {
return context.getSharedPreferences("app_preference_key"), Context.MODE_PRIVATE);
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
这是首选项的单例实现。