我有以下代码:
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);
当我选中/取消选中相应的框时,我得到了结果错误:
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)
你正在改变onSharedPreferenceChanged
内部的共享偏好,不要这样做,它是无限递归的。