检索值时,SharedPreferences返回null

时间:2019-02-14 10:12:41

标签: java android sharedpreferences

我试图在应用关闭或被销毁时将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 个答案:

答案 0 :(得分: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"));
        }}
}

希望它会对您有所帮助。