android – 保存首选项

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

我在我的游戏中有方法writeSettings(),当我想通过readSettings()方法加载最后一个游戏时,我试图保存一些变量来使用它.有一些参数,如第一(第二)PlayerName,第一(第二)Playerscore等.我正在尝试使用SharedPreferences来保存它们,但是在启动程序时我得到“强制关闭”对话框.

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);

解决方法

我是这样做的:
Editor editor = PreferenceManager.getDefaultSharedPreferences(Context).edit();
                editor.putString("firstPlayerNameSave",firstPlayerNameTextView.getText().toString());              
                editor.commit();

阅读:

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

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...