【发布时间】:2021-08-20 16:53:57
【问题描述】:
我有一个片段,我想调用一个 AppConfig.class 中的方法。但是当我运行应用程序时,出现了" Attempt to invoke interface method 'android.content.SharedPreferences$Editor android.content.SharedPreferences.edit()' on a null object reference" 的错误。如何解决此错误?
这是我在 AppConfig.class 中的代码
public void updateUserLoginStatus(boolean status){
SharedPreferences.Editor editor= sharedPreferences.edit();
editor.putBoolean(context.getString(R.string.pref_is_user_login),status);
editor.apply();
}
这是我在 Fragment 中的代码
appConfig.updateUserLoginStatus(false);
【问题讨论】:
-
sharedPreferences 为空。
-
你能分享一下共享偏好初始化吗?
-
使用
editor.commit();代替editor.apply();
标签: android android-studio android-fragments