Save preferences
I'm a beginner at Android programming, unfortunately I have a problem :)
I have method writeSettings() in my game in which I'm trying to save some variables to use it when want to load last game by readSettings() method. There are parameters such as first(second)PlayerName, first(second)PlayerScore and so on. I'm trying to use SharedPreferences to save them, but I get "Force Close" dialog, when starting program.
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);
}
How I could know, problem appears in first entry of writeSettings(). But I don't know what I'm doing wrong?
Sorry for my English.
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(Met开发者_如何学Gohod.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 is a SharedPreferences preferences = getPreferences(MODE_PRIVATE);
This is how I do it:
Editor editor = PreferenceManager.getDefaultSharedPreferences(Context).edit();
editor.putString("firstPlayerNameSave", firstPlayerNameTextView.getText().toString());
editor.commit();
And to read:
firstPlayerNameTextView.setText(PreferenceManager.getDefaultSharedPreferences(Context).getString("firstPlayerNameSave", ""));
What I do is almost identical to what martipelant suggested.
Getting the editor:
Editor preferenceEditor = getSharedPreferences("com.mycompany.android.myapp",MODE_PRIVATE).edit();
And reading:
mSharedPreferences = getSharedPreferences("com.mycompany.android.myapp",Context.MODE_PRIVATE);
AFAIK Activity.getPreferences()
is a wrapper for Context.getSharedPreferences(String, int)
where String is the name of the activity.
It should be used only for accessing preferences that are private to this activity.
Try to change the code
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
to
SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(context);
精彩评论