0
我试图为我的Hangman游戏创建一个endgame活动,并且我有一些提交的值比字符串更麻烦。将boolean和int保存到sharedPrefs时遇到问题
这是我的主要活动:
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;
}
我得到sharedprefs这样的:
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。
有人能帮我解释一下吗?
哇,这很容易。非常感谢快速回复。 – SQDK 2010-12-11 11:12:28