将 boolean 和 int 保存到共享首选项时遇到问题

发布于 2024-10-07 13:51:23 字数 1713 浏览 0 评论 0原文

我正在尝试为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绮烟 2024-10-14 13:51:23

如果您只想将数据从一个活动移动到另一个活动,我会将其附加到意图。
将其写入共享内存意味着访问手机存储,而且速度非常慢。

你可以这样做:

Intent intent = new Intent(HangmanGameActivity.this, HangmanEndActivity.class);
intent.putExtra(GAME_LOGIC_SCORE_STRING, tries + " of " + numberOfLives + " used"):
intent.putExtra("GAME_LOGIC_WIN_LOOSE", true);  
startActivity(intent);

在 EndActivity 中你可以这样做:

Intent intent = getIntent();
String gameString = intent.getStringExtra(GAME_LOGIC_SCORE_STRING, "default value");
boolean win = intent.getBooleanExtra(GAME_LOGIC_WIN_LOOSE, false);

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:

Intent intent = new Intent(HangmanGameActivity.this, HangmanEndActivity.class);
intent.putExtra(GAME_LOGIC_SCORE_STRING, tries + " of " + numberOfLives + " used"):
intent.putExtra("GAME_LOGIC_WIN_LOOSE", true);  
startActivity(intent);

In the EndActivity you would do:

Intent intent = getIntent();
String gameString = intent.getStringExtra(GAME_LOGIC_SCORE_STRING, "default value");
boolean win = intent.getBooleanExtra(GAME_LOGIC_WIN_LOOSE, false);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文