将 boolean 和 int 保存到共享首选项时遇到问题
我正在尝试为 Android 的 Hangman 游戏创建一个游戏结束活动,但在提交字符串以外的值时遇到了一些问题。
这是我的主要活动:
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。
有人可以帮我解释一下吗?
I'm trying to create an endgame activity for my Hangman game for android and i'm having some trouble committing values OTHER than strings.
Here is my main activity:
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;
}
I get the sharedprefs like this:
mGameSettings = getSharedPreferences("GAME_PREFERENCES", Context.MODE_PRIVATE);
And this is where something goes wrong when committing the changes to the editor:
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));
}
And after changing activity i refetch the values like this:
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);
}
}
But somehow only the String is being committed correctly. I guess the boolean value reverts to the default value of false.
Could someone help me shed some light on this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您只想将数据从一个活动移动到另一个活动,我会将其附加到意图。
将其写入共享内存意味着访问手机存储,而且速度非常慢。
你可以这样做:
在 EndActivity 中你可以这样做:
If you only want to move the data from one activity to another I would attach it to the Intent.
Writing it to shared memory means accessing the phone storage and that is really slow.
You could it do in this way:
In the EndActivity you would do: