保存首选项

时间:2010-12-23 10:10:01

标签: android

我是Android编程的初学者,不幸的是我有一个问题:)

我的游戏中有 writeSettings()方法,我想通过 readSettings()方法加载最后一个游戏时保存一些变量来使用它。有一些参数,例如第一(第二)PlayerName,第一(第二)PlayerScore 等等。我正在尝试使用SharedPreferences来保存它们,但是在启动程序时我会收到“Force Close”对话框。

SharedPreferences preferences = getPreferences(MODE_PRIVATE); //Global variable

    private void writeSettings() {
                SharedPreferences.Editor editor = preferences.edit();
                editor.putInt("timeSave", time);
                editor.putString("firstPlayerNameSave", firstPlayerNameTextView.getText().toString());
                editor.putString("secondPlayerNameSave", secondPlayerNameTextView.getText().toString());
                editor.putString("firstPlayerScoreSave", firstPlayerScoreTextView.getText().toString());
                editor.putString("secondPlayerScoreSave", secondPlayerScoreTextView.getText().toString());
                editor.putInt("nowPlayerSave", nowPlayer);        
                editor.commit();

        }


       private void readSettings() {
    //"time" parameter program reads in another method
                    firstPlayerNameTextView.setText(preferences.getString("firstPlayerNameSave", ""));
                    secondPlayerNameTextView.setText(preferences.getString("secondPlayerNameSave", ""));
                    firstPlayerScoreTextView.setText(preferences.getString("firstPlayerScoreSave", ""));
                    secondPlayerScoreTextView.setText(preferences.getString("secondPlayerScoreSave", ""));
                    nowPlayer = preferences.getInt("nowPlayerSave", -1);

            }

我怎么知道,问题出现在writeSettings()的第一个条目中。 但我不知道我做错了什么?

抱歉我的英文。

UPD。

12-23 16:23:31.334: ERROR/AndroidRuntime(410): Uncaught handler: thread main exiting due to uncaught exception
12-23 16:23:31.520: ERROR/AndroidRuntime(410): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.game/com.game.Game}: java.lang.NullPointerException
12-23 16:23:31.520: ERROR/AndroidRuntime(410):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
12-23 16:23:31.520: ERROR/AndroidRuntime(410):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
12-23 16:23:31.520: ERROR/AndroidRuntime(410):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
12-23 16:23:31.520: ERROR/AndroidRuntime(410):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
12-23 16:23:31.520: ERROR/AndroidRuntime(410):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-23 16:23:31.520: ERROR/AndroidRuntime(410):     at android.os.Looper.loop(Looper.java:123)
12-23 16:23:31.520: ERROR/AndroidRuntime(410):     at android.app.ActivityThread.main(ActivityThread.java:4363)
12-23 16:23:31.520: ERROR/AndroidRuntime(410):     at java.lang.reflect.Method.invokeNative(Native Method)
12-23 16:23:31.520: ERROR/AndroidRuntime(410):     at java.lang.reflect.Method.invoke(Method.java:521)
12-23 16:23:31.520: ERROR/AndroidRuntime(410):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-23 16:23:31.520: ERROR/AndroidRuntime(410):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-23 16:23:31.520: ERROR/AndroidRuntime(410):     at dalvik.system.NativeStart.main(Native Method)
12-23 16:23:31.520: ERROR/AndroidRuntime(410): Caused by: java.lang.NullPointerException
12-23 16:23:31.520: ERROR/AndroidRuntime(410):     at android.content.ContextWrapper.getPackageName(ContextWrapper.java:120)
12-23 16:23:31.520: ERROR/AndroidRuntime(410):     at android.app.Activity.getLocalClassName(Activity.java:3410)
12-23 16:23:31.520: ERROR/AndroidRuntime(410):     at android.app.Activity.getPreferences(Activity.java:3444)
12-23 16:23:31.520: ERROR/AndroidRuntime(410):     at com.game.Game.<init>(Game.java:69)
12-23 16:23:31.520: ERROR/AndroidRuntime(410):     at java.lang.Class.newInstanceImpl(Native Method)
12-23 16:23:31.520: ERROR/AndroidRuntime(410):     at java.lang.Class.newInstance(Class.java:1479)
12-23 16:23:31.520: ERROR/AndroidRuntime(410):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
12-23 16:23:31.520: ERROR/AndroidRuntime(410):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2409)
12-23 16:23:31.520: ERROR/AndroidRuntime(410):     ... 11 more

Game.java:69是SharedPreferences preferences = getPreferences(MODE_PRIVATE);

3 个答案:

答案 0 :(得分:14)

我就是这样做的:

Editor editor = PreferenceManager.getDefaultSharedPreferences(Context).edit();
                editor.putString("firstPlayerNameSave", firstPlayerNameTextView.getText().toString());              
                editor.commit();

并阅读:

firstPlayerNameTextView.setText(PreferenceManager.getDefaultSharedPreferences(Context).getString("firstPlayerNameSave", ""));

答案 1 :(得分:1)

我所做的几乎与martipelant建议的相同。

获得编辑:

Editor preferenceEditor = getSharedPreferences("com.mycompany.android.myapp",MODE_PRIVATE).edit();

阅读:

       mSharedPreferences = getSharedPreferences("com.mycompany.android.myapp",Context.MODE_PRIVATE);

答案 2 :(得分:1)

AFAIK Activity.getPreferences()Context.getSharedPreferences(String, int)的包装器,其中String是活动的名称。 它应仅用于访问此活动专用的首选项。 尝试更改代码

SharedPreferences preferences = getPreferences(MODE_PRIVATE);

SharedPreferences preferences = PreferenceManager
                .getDefaultSharedPreferences(context);