SharedPreference not saving

78 views Asked by At

Hi I would like to ask why is that my sharedPreference on EditTextPreference is not saving here is my code

    SharedPreferences coinPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    String coin_pref = coinPrefs.getString("currentCoin", null);
    Editor editor = coinPrefs.edit();

    if (Answer.equals("LEFTOVERS")){
        bg1 = 1;
        Coin = Integer.parseInt(coin_pref) + 3;
        coin_pref = String.valueOf(Coin);
        tvCoin.setText(coin_pref);
        editor.putString("coinValue", coin_pref);
        editor.commit();

    }

what I'm trying to do here is get the Coin value which is 5 then add 3 on it then put it in the coin_pref then put it in the sharedPreference. I noticed that my tvCoin.setText which is 5 became 8 but still didn't save it in the EditTextPreference. Thanks in advance for the help

EDITED

I tried using this code

SharedPreferences coinPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    Integer coin_pref = coinPrefs.getInt("currentCoin", Coin);
    Editor editor = coinPrefs.edit();

    if (Answer.equals("LEFTOVERS")){
        bg1 = 1;
        Coin = coin_pref + 3;
        coin_pref = Coin;
        tvCoin.setText(coin_pref);
        editor.putInt("coinValue", coin_pref);
        editor.commit();
    }

and got this on logcat

11-30 21:26:55.691: D/dalvikvm(483): GC_FOR_MALLOC freed 2859 objects / 177440 bytes in 99ms
11-30 21:27:03.963: D/AndroidRuntime(483): Shutting down VM
11-30 21:27:03.971: W/dalvikvm(483): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
11-30 21:27:03.971: E/AndroidRuntime(483): FATAL EXCEPTION: main
11-30 21:27:03.971: E/AndroidRuntime(483): java.lang.NullPointerException
11-30 21:27:03.971: E/AndroidRuntime(483):  at com.thesis.logipic.Gameplay.beginnerChecking(Gameplay.java:501)
11-30 21:27:03.971: E/AndroidRuntime(483):  at com.thesis.logipic.Gameplay$1.onClick(Gameplay.java:434)
11-30 21:27:03.971: E/AndroidRuntime(483):  at android.view.View.performClick(View.java:2408)
11-30 21:27:03.971: E/AndroidRuntime(483):  at android.view.View$PerformClick.run(View.java:8816)
11-30 21:27:03.971: E/AndroidRuntime(483):  at android.os.Handler.handleCallback(Handler.java:587)
11-30 21:27:03.971: E/AndroidRuntime(483):  at android.os.Handler.dispatchMessage(Handler.java:92)
11-30 21:27:03.971: E/AndroidRuntime(483):  at android.os.Looper.loop(Looper.java:123)
11-30 21:27:03.971: E/AndroidRuntime(483):  at android.app.ActivityThread.main(ActivityThread.java:4627)
11-30 21:27:03.971: E/AndroidRuntime(483):  at java.lang.reflect.Method.invokeNative(Native Method)
11-30 21:27:03.971: E/AndroidRuntime(483):  at java.lang.reflect.Method.invoke(Method.java:521)
11-30 21:27:03.971: E/AndroidRuntime(483):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-30 21:27:03.971: E/AndroidRuntime(483):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-30 21:27:03.971: E/AndroidRuntime(483):  at dalvik.system.NativeStart.main(Native Method)
11-30 21:27:11.432: I/Process(483): Sending signal. PID: 483 SIG: 9
0

There are 0 answers