3

I have the following code:

sP = PreferenceManager.getDefaultSharedPreferences(this);

OnSharedPreferenceChangeListener listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
    @Override
    public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
        if (key.equalsIgnoreCase("checkbox_radio")){

            if (sP.getBoolean(key, true)){
                Editor editor = sP.edit();
                editor.putBoolean(key, false);
                editor.commit();
            }
            else {
                Editor editor = sP.edit();
                editor.putBoolean(key, true);
                editor.commit();
            }
        }
    }
};

sP.registerOnSharedPreferenceChangeListener(listener);

I am getting the resulting error when I check/uncheck the corresponding box:

03-10 22:24:50.883: D/dalvikvm(3616): GC_CONCURRENT freed 5171K, 36% free 9629K/14936K, paused 5ms+3ms, total 35ms
03-10 22:24:51.313: D/dalvikvm(3616): GC_CONCURRENT freed 725K, 36% free 9593K/14936K, paused 5ms+4ms, total 34ms
03-10 22:24:51.773: D/dalvikvm(3616): GC_CONCURRENT freed 684K, 36% free 9584K/14936K, paused 1ms+2ms, total 22ms
03-10 22:24:52.203: D/dalvikvm(3616): GC_CONCURRENT freed 653K, 36% free 9619K/14936K, paused 5ms+3ms, total 31ms
03-10 22:24:52.623: D/dalvikvm(3616): GC_CONCURRENT freed 683K, 36% free 9609K/14936K, paused 3ms+2ms, total 25ms
03-10 22:24:53.023: D/dalvikvm(3616): GC_CONCURRENT freed 671K, 36% free 9652K/14936K, paused 3ms+4ms, total 33ms
03-10 22:24:53.083: I/dalvikvm(3616): threadid=1: stack overflow on call to Ljava/lang/Thread;.currentThread:L
03-10 22:24:53.083: I/dalvikvm(3616):   method requires 4+20+0=24 bytes, fp is 0x61f7630c (12 left)
03-10 22:24:53.083: I/dalvikvm(3616):   expanding stack end (0x61f76300 to 0x61f76000)
03-10 22:24:53.083: I/dalvikvm(3616): Shrank stack (to 0x61f76300, curFrame is 0x61f764a0)
03-10 22:24:53.083: D/AndroidRuntime(3616): Shutting down VM
03-10 22:24:53.083: W/dalvikvm(3616): threadid=1: thread exiting with uncaught exception (group=0x40e48930)
03-10 22:24:53.153: D/dalvikvm(3616): GC_CONCURRENT freed 642K, 35% free 9803K/14936K, paused 3ms+2ms, total 23ms
03-10 22:24:53.153: D/dalvikvm(3616): WAIT_FOR_CONCURRENT_GC blocked 8ms
03-10 22:24:53.153: E/AndroidRuntime(3616): FATAL EXCEPTION: main
03-10 22:24:53.153: E/AndroidRuntime(3616): java.lang.StackOverflowError
03-10 22:24:53.153: E/AndroidRuntime(3616):     at java.lang.ThreadLocal.get(ThreadLocal.java:53)
03-10 22:24:53.153: E/AndroidRuntime(3616):     at dalvik.system.BlockGuard.getThreadPolicy(BlockGuard.java:140)
03-10 22:24:53.153: E/AndroidRuntime(3616):     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:106)
03-10 22:24:53.153: E/AndroidRuntime(3616):     at libcore.io.IoBridge.open(IoBridge.java:400)
03-10 22:24:53.153: E/AndroidRuntime(3616):     at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
03-10 22:24:53.153: E/AndroidRuntime(3616):     at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
03-10 22:24:53.153: E/AndroidRuntime(3616):     at android.app.SharedPreferencesImpl.createFileOutputStream(SharedPreferencesImpl.java:543)
03-10 22:24:53.153: E/AndroidRuntime(3616):     at android.app.SharedPreferencesImpl.writeToFile(SharedPreferencesImpl.java:591)
03-10 22:24:53.153: E/AndroidRuntime(3616):     at android.app.SharedPreferencesImpl.access$800(SharedPreferencesImpl.java:52)
Jeff Yates
  • 61,417
  • 20
  • 137
  • 189
user1830769
  • 45
  • 1
  • 5

1 Answers1

2

You are changing the shared preference inside of onSharedPreferenceChanged, dont do this, its infinite recursion.

smith324
  • 13,020
  • 9
  • 37
  • 58
  • How am I supposed to save the changes made then? – user1830769 Mar 11 '13 at 03:36
  • `editor.commit();` saves the changes. `onSharedPreferenceChanged` is called after changes have been made – smith324 Mar 11 '13 at 03:37
  • Sorry, I am still not completely clear. Are you saying that I dont need a listener at all and can just do editor.commit() when they click done? I am still unsure of how to go about how and where to do this. I had it working fine for a checkbox but don't know what to do for a checkboxpreference – user1830769 Mar 11 '13 at 03:40
  • I just realized with checkboxpreference it seems like the preferences are automatically remembered. Thank you for the quick responses. – user1830769 Mar 11 '13 at 03:46
  • @user1830769 How could you solve this issue? Where did you save your changes? – nAkhmedov Feb 24 '16 at 12:18