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