问题描述
我试图在应用程序关闭或销毁时使用SharedPreferences
将一些值保存为键值对,并在应用程序再次打开时检索它们。
问题是当我调用检索值时,它返回null
。
我尝试传递上下文或获取应用程序上下文并使值static
,但它似乎不起作用。
class SavedValues {
private static final String keyP1fName = String.valueOf(R.string.player_1_name_key);
private static final String keyP1Game = String.valueOf(R.string.player_1_game_key);
private static final String keyP2fName = String.valueOf(R.string.player_2_sname_key);
//Have other strings here
private MainGame mainGame;
SavedValues(MainGame mainGame) {
this.mainGame = mainGame;
}
//private SharedPreferences mPrefs = mainGame.getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
private SharedPreferences mPrefs = mainGame.getPreferences(Context.MODE_PRIVATE);
//SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
void saveMainGameValues() {
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString(keyP1fName, mainGame.fullNameP1.getText().toString());
editor.putString(keyP1Game, mainGame.gameScoreP1.getText().toString());
editor.putString(keyP2fName, mainGame.fullNameP2.getText().toString());
editor.putString(keyP2Game, maingGame.gameScoreP2.getText().toString());
editor.apply();
}
void getMainGameValues() {
mPrefs = mainGame.getPreferences(Context.MODE_PRIVATE);
if (mPrefs.contains(keyP1fName)) {
mainGame.fullNameP1.setText(mPrefs.getString(keyP1fName, "Player 1"));
mainGame.gameScoreP1.setText(mPrefs.getString(keyP1Game, "0"));
mainGame.fullNameP2.setText(mPrefs.getString(keyP2fName, "Player 2"));
mainGame.gameScoreP2.setText(mPrefs.getString(keyP2Game, "0"));
}}
}
我试图在我的主要活动的onStart/onStop等方法中调用这些
@Override
protected void onStart() {
//savedVals.getMainGameValues();
super.onStart();
}
@Override
protected void onResume() {
savedVals.getMainGameValues();
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
savedVals.saveMainGameValues();
}
@Override
protected void onStop() {
super.onStop();
savedVals.saveMainGameValues();
}
@Override
protected void onDestroy() {
super.onDestroy();
savedVals.saveMainGameValues();
}
我应该获取最后检索到的值或至少是默认值,但应用程序崩溃并在 onStart 或 onResume 中为回调获取以下错误:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.andygix.a40love.SavedValues.getMainGameValues()' on a null object reference
at com.andygix.a40love.MainGame.onResume(MainGame.java:379)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1364)
at android.app.Activity.performResume(Activity.java:7490)
1楼
您SavedValues
类有逻辑错误。
当您的类将被初始化时,构造函数将被调用并且mainGame
获得一些值,但在此之前您创建了mPref
变量,因此它没有值。
您必须在构造函数中初始化mPref
。
在这里,我更新了课程.. 尝试更换和检查。
class SavedValues {
private static final String keyP1fName = String.valueOf(R.string.player_1_name_key);
private static final String keyP1Game = String.valueOf(R.string.player_1_game_key);
private static final String keyP2fName = String.valueOf(R.string.player_2_sname_key);
private MainGame mainGame;
/**************************** THIS PART I CHANGED *******************/
private SharedPreferences mPrefs;
SavedValues(MainGame mainGame) {
this.mainGame = mainGame;
mPref = mainGame.getPreferences(Context.MODE_PRIVATE);
}
/**********************************************/
//private SharedPreferences mPrefs = mainGame.getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
//SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
void saveMainGameValues() {
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString(keyP1fName, mainGame.fullNameP1.getText().toString());
editor.putString(keyP1Game, mainGame.gameScoreP1.getText().toString());
editor.putString(keyP2fName, mainGame.fullNameP2.getText().toString());
editor.putString(keyP2Game, maingGame.gameScoreP2.getText().toString());
editor.apply();
}
void getMainGameValues() {
mPrefs = mainGame.getPreferences(Context.MODE_PRIVATE);
if (mPrefs.contains(keyP1fName)) {
mainGame.fullNameP1.setText(mPrefs.getString(keyP1fName, "Player 1"));
mainGame.gameScoreP1.setText(mPrefs.getString(keyP1Game, "0"));
mainGame.fullNameP2.setText(mPrefs.getString(keyP2fName, "Player 2"));
mainGame.gameScoreP2.setText(mPrefs.getString(keyP2Game, "0"));
}}
}
希望它会帮助你。