Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save preferences

Tags:

android

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(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 is a SharedPreferences preferences = getPreferences(MODE_PRIVATE);

like image 831
pavelkorolevxyz Avatar asked Dec 23 '10 10:12

pavelkorolevxyz


People also ask

How do I save preferences on Android?

One of this way is called Shared Preferences. Shared Preferences allow you to save and retrieve data in the form of key,value pair. In order to use shared preferences, you have to call a method getSharedPreferences() that returns a SharedPreference instance pointing to the file that contains the values of preferences.

Where are preferences stored in Android?

Android stores Shared Preferences settings as XML file in shared_prefs folder under DATA/data/{application package} directory. The DATA folder can be obtained by calling Environment. getDataDirectory() .

What are shared preferences in Android?

A SharedPreferences object points to a file containing key-value pairs and provides simple methods to read and write them. Each SharedPreferences file is managed by the framework and can be private or shared. This page shows you how to use the SharedPreferences APIs to store and retrieve simple values.

Is shared preferences deprecated?

Yes, it is deprecated. Use the AndroidX Preference Library for consistent behavior across all devices. For more information on using the AndroidX Preference Library see Settings.


2 Answers

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", ""));
like image 197
martinpelant Avatar answered Sep 28 '22 21:09

martinpelant


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);
like image 38
Michiel Avatar answered Sep 28 '22 22:09

Michiel