开发者

Android crashes because of 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:

    highSco开发者_如何学Pythonres = 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 :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜