Android 由于 SharedPreferences 而崩溃

发布于 2024-12-05 20:16:59 字数 1333 浏览 0 评论 0原文

我是 Android 的初学者,但我已经对这个问题做了所有的研究,但我似乎找不到解决它的方法...该应用程序运行良好,直到我决定将 SharedPreferences 添加到为了更新一些高分。现在我似乎无法让它工作,它总是在一开始就崩溃(在启动屏幕之后)

在 on Create 方法中,在 setContentView 下面我有以下几行:

    highScores = getSharedPreferences("HighScores", 0);
    hs1 = highScores.getInt("HighScore1", 0);
    shs1 = highScores.getString("sHighScore1", "User1");

还有几行,但大多数都是备用的前面代码中的第 2 行和第 3 行。我已在类的主体中将 highScores 声明为 SharedPreferences。在此类中,我使用从 SharedPreferences 收集的信息的唯一地方是编辑 TextView,它使用以下代码:

    High1.setText(String.format("1. %04d - %s", hs1, shs1));

我的猜测是我在代码中的某个地方犯了错误,但无法找到它...

只是有关附加信息,我只在另一个类中使用 SharedPreferences (它应该在游戏结束时更新高分)并使用类似的代码:

    highScores = getSharedPreferences("HighScores", 0);
    hs1 = highScores.getInt("HighScore1", 0);
    shs1 = highScores.getString("sHighScore1", "User1");

上面的代码用于获取以前的高分,而下面的代码用于更新:

    prefEditor.putInt("HighScore1", hs1);
    prefEditor.putString("sHighScore1", shs1);
    prefEditor.commit();

我已经声明了SharedPrefs 编辑器为:

    SharedPreferences.Editor prefEditor = highScores.edit();

我真的很感激任何帮助,因为我似乎找不到我做错了什么,可能是某个地方的一个小错误,但这真的让我发疯:PI 会给你更多的代码,但是我看不到其中的任何目的,因为它不使用 SharedPreferences 并且我很确定它们是问题的原因...

提前感谢您的帮助:)

I'm a beginner in Android, but I've done all the research I could on this problem and I just can't seem to find a way to solve it... The app worked fine until I decided to add the SharedPreferences in order to update some high scores. Now I can't seem to make it work, it always crashes at the very start (after the splash screen)

In the on Create method, below the setContentView I have the following lines:

    highScores = getSharedPreferences("HighScores", 0);
    hs1 = highScores.getInt("HighScore1", 0);
    shs1 = highScores.getString("sHighScore1", "User1");

and a couple more, but mostle they are alternate takes on lines 2 and 3 in the prior code. I have declared the highScores as SharedPreferences in the body of the class. The only place in this class where I use the info gathered from the SharedPreferences is in editing a TextView, which uses the following code:

    High1.setText(String.format("1. %04d - %s", hs1, shs1));

My guess is that I made an error somewhere in the code, but am unable to find it...

Just for additional info, I only use the SharedPreferences in one other class (which should update the high scores at the end of games) and uses a simillar code:

    highScores = getSharedPreferences("HighScores", 0);
    hs1 = highScores.getInt("HighScore1", 0);
    shs1 = highScores.getString("sHighScore1", "User1");

The code above was for getting previous high scores, while the code below is used for updating:

    prefEditor.putInt("HighScore1", hs1);
    prefEditor.putString("sHighScore1", shs1);
    prefEditor.commit();

I have declared the SharedPrefs editor as:

    SharedPreferences.Editor prefEditor = highScores.edit();

I would really appreciate any help, as I can't seem to find what I've done wrong, probably it's a small error somewhere, but it's really driving me nuts :P I would give you more of the code, but I can't see any purpose in that, because it doesn't use the SharedPreferences and I'm pretty sure they're the cause of the issue...

Thank you in advance for your help :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文