android – 保存首选项

前端之家收集整理的这篇文章主要介绍了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",""));
原文链接:https://www.f2er.com/android/317226.html

猜你在找的Android相关文章