我正在尝试为我的 Android 刽子手游戏创建一个残局活动,但我在提交字符串以外的值时遇到了一些问题。
这是我的主要活动:
package com.assignment.hangman;
import android.app.Activity;
public class HangmanActivity extends Activity {
public static final String GAME_PREFERENCES = "Game Preferences";
public static final String GAME_LOGIC = "Game Logic";
public static final String GAME_LOGIC_GUESS = "Guessed letter";
public static final String GAME_LOGIC_SCORE_STRING = "Unknow score";
public static final boolean GAME_LOGIC_WIN_LOOSE = false;
}
我得到这样的共享首选项:
mGameSettings = getSharedPreferences("GAME_PREFERENCES", Context.MODE_PRIVATE);
这就是将更改提交到编辑器时出现问题的地方:
public void finishGame() {
//Commit different game variables so they can be used in the end game activity
Editor editor = mGameSettings.edit();
editor.putString(GAME_LOGIC_SCORE_STRING, (tries + " of " + numberOfLives + " used"));
if (tries != numberOfLives){
editor.putBoolean("GAME_LOGIC_WIN_LOOSE", true);
}
editor.commit();
// Launch end game Activity
startActivity(new Intent(HangmanGameActivity.this, HangmanEndActivity.class));
}
在更改活动后,我重新获取如下值:
if (mGameSettings.contains("GAME_LOGIC_WIN_LOOSE")) {
Log.i(GAME_DEBUG, "Succes");
boolean winLoose = mGameSettings.getBoolean("GAME_LOGIC_WIN_LOOSE", false);
if (winLoose) {
winLooseView.setText(R.string.you_win);
} else {
winLooseView.setText(R.string.you_loose);
}
}
但不知何故,只有字符串被正确提交。我猜布尔值会恢复为默认值 false。
有人可以帮我解释一下吗?