使用共享首选项在Android游戏中保存高分

时间:2014-06-12 16:58:31

标签: java android sharedpreferences

我制作了一款Android游戏并希望使用共享偏好保存高分。我创建了一个extends Application但在<:p>上收到空指针异常的类

SharedPreferences prefs = getSharedPreferences(MY_PREFERENCES, Context.MODE_PRIVATE);

我很欣赏一些关于如何保存和加载共享首选项的示例代码,因为其他示例导致此空指针异常。

06-12 17:53:55.864: E/AndroidRuntime(13923): FATAL EXCEPTION: Thread-17335
06-12 17:53:55.864: E/AndroidRuntime(13923): Process: com.nunninatorapps.magiciangame, PID: 13923
06-12 17:53:55.864: E/AndroidRuntime(13923): java.lang.NullPointerException
06-12 17:53:55.864: E/AndroidRuntime(13923):    at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:173)
06-12 17:53:55.864: E/AndroidRuntime(13923):    at com.nunninatorapps.magiciangame.MyApplication.save(MyApplication.java:22)
06-12 17:53:55.864: E/AndroidRuntime(13923):    at com.nunninatorapps.magiciangame.GameScreen.drawGameOverUI(GameScreen.java:609)
06-12 17:53:55.864: E/AndroidRuntime(13923):    at com.nunninatorapps.magiciangame.GameScreen.paint(GameScreen.java:493)
06-12 17:53:55.864: E/AndroidRuntime(13923):    at com.nunninatorapps.framework.implementation.AndroidFastRenderView.run(AndroidFastRenderView.java:48)
06-12 17:53:55.864: E/AndroidRuntime(13923):    at java.lang.Thread.run(Thread.java:841)

3 个答案:

答案 0 :(得分:0)

private static SharedPreferences.Editor mPrefsEditor;
int mylevel
在oncreate中

SharedPreferences prefs = getSharedPreferences(Global.PREFERENCE_NAME,
                MODE_PRIVATE);
loadSavedPrefs();

我的加载方法

private void loadSavedPrefs() {
        SharedPreferences sharedPreferences = PreferenceManager

        .getDefaultSharedPreferences(this);
//first value is the key or name value is under. second value is the default value if their //is no instance of this key already.
        mylevel = sharedPreferences.getInt("level", 10);


    }

保存看起来像这样

private void save() {
        SharedPreferences sharedPreferences = PreferenceManager

        .getDefaultSharedPreferences(this);

        Editor editor = sharedPreferences.edit();


        editor.putInt("level", mylevel);

        editor.commit();

        Toast.makeText(getBaseContext(), "Data Saved...", Toast.LENGTH_SHORT).show();

    }

最好在暂停时调用保存(所以它在关闭时保存,但对于高分,你可以通过任何方法调用它,例如当游戏结束并显示胜利屏幕时

@Override
    protected void onPause() {
        save();

        super.onPause();
    }

答案 1 :(得分:0)

可以很容易地完成几行:

SharedPreferences preferences = getSharedPreferences(APP_NAME, MODE_PRIVATE);
preferences.edit().putInt(SELECTED_POSITION, position).commit();

APP_NAME是一个字符串,SELECTED_POSITION是另一个字符串,并且我想存储一个int。

得到它:

SharedPreferences preferences = getSharedPreferences(APP_NAME, MODE_PRIVATE);
preferences.getInt(SELECTED_POSITION, 0);

答案 2 :(得分:0)

阅读有关SharedPreferences

的文档
SharedPreferences prefs = this.getSharedPreferences("your_app_name_string", Context.MODE_PRIVATE);

然后你可以设置像HashMap这样的值

prefs.edit().putInt("score", points).commit(); //"score" is a key and 'points' is a variable set by you.

要检索你可以使用

prefs.getInt("score", 0); //You pass the string you used as key and a default value.

HIH