public class SettingsActivity extends SherlockPreferenceActivity implements OnSharedPreferenceChangeListener, OnPreferenceChangeListener {
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
ListPreference theme = (ListPreference) getPreferenceScreen().findPreference("theme");
theme.setSummary(theme.getValue().toString());
theme.setOnPreferenceChangeListener(this);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
getSupportActionBar().setTitle(R.string.menu_preferences);
}
@SuppressWarnings("deprecation")
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
if (key.equals("theme")) {
SharedPreferences.Editor editor = sharedPreferences.edit();
ListPreference theme = (ListPreference) getPreferenceScreen().findPreference("theme");
theme.setSummary(theme.getEntry());
editor.putString("theme", theme.getEntry().toString());
editor.commit();
}
}
public boolean onPreferenceChange(Preference preference, Object newValue) {
ListPreference theme = (ListPreference) preference;
theme.setSummary(theme.getEntry());
return true;
}
}
,當我得到這個:StackOverFlowException commiting SharedPreferences
07-03 09:46:22.563: E/AndroidRuntime(421): java.lang.StackOverflowError
07-03 09:46:22.563: E/AndroidRuntime(421): at java.nio.BaseByteBuffer.<init>(BaseByteBuffer.java:26)
07-03 09:46:22.563: E/AndroidRuntime(421): at java.nio.HeapByteBuffer.<init>(HeapByteBuffer.java:49)
07-03 09:46:22.563: E/AndroidRuntime(421): at java.nio.HeapByteBuffer.<init>(HeapByteBuffer.java:41)
07-03 09:46:22.563: E/AndroidRuntime(421): at java.nio.ReadWriteHeapByteBuffer.<init>(ReadWriteHeapByteBuffer.java:43)
07-03 09:46:22.563: E/AndroidRuntime(421): at java.nio.BufferFactory.newByteBuffer(BufferFactory.java:38)
07-03 09:46:22.563: E/AndroidRuntime(421): at java.nio.ByteBuffer.wrap(ByteBuffer.java:82)
07-03 09:46:22.563: E/AndroidRuntime(421): at java.nio.charset.CharsetEncoder.isLegalReplacement(CharsetEncoder.java:646)
07-03 09:46:22.563: E/AndroidRuntime(421): at java.nio.charset.CharsetEncoder.replaceWith(CharsetEncoder.java:752)
07-03 09:46:22.563: E/AndroidRuntime(421): at java.nio.charset.CharsetEncoder.<init>(CharsetEncoder.java:184)
07-03 09:46:22.563: E/AndroidRuntime(421): at com.ibm.icu4jni.charset.CharsetEncoderICU.<init>(CharsetEncoderICU.java:75)
07-03 09:46:22.563: E/AndroidRuntime(421): at com.ibm.icu4jni.charset.CharsetICU.newEncoder(CharsetICU.java:74)
07-03 09:46:22.563: E/AndroidRuntime(421): at com.android.internal.util.FastXmlSerializer.setOutput(FastXmlSerializer.java:292)
07-03 09:46:22.563: E/AndroidRuntime(421): at com.android.internal.util.XmlUtils.writeMapXml(XmlUtils.java:181)
07-03 09:46:22.563: E/AndroidRuntime(421): at android.app.ContextImpl$SharedPreferencesImpl.writeFileLocked(ContextImpl.java:2889)
07-03 09:46:22.563: E/AndroidRuntime(421): at android.app.ContextImpl$SharedPreferencesImpl.access$300(ContextImpl.java:2637)
07-03 09:46:22.563: E/AndroidRuntime(421): at android.app.ContextImpl$SharedPreferencesImpl$EditorImpl.commit(ContextImpl.java:2822)
07-03 09:46:22.563: E/AndroidRuntime(421): at com.centauri.equations.settings.SettingsActivity.onSharedPreferenceChanged(SettingsActivity.java:56)
07-03 09:46:22.563: E/AndroidRuntime(421): at android.app.ContextImpl$SharedPreferencesImpl$EditorImpl.commit(ContextImpl.java:2830)
07-03 09:46:22.563: E/AndroidRuntime(421): at com.centauri.equations.settings.SettingsActivity.onSharedPreferenceChanged(SettingsActivity.java:56)
07-03 09:46:22.563: E/AndroidRuntime(421): at android.app.ContextImpl$SharedPreferencesImpl$EditorImpl.commit(ContextImpl.java:2830)
07-03 09:46:22.563: E/AndroidRuntime(421): at com.centauri.equations.settings.SettingsActivity.onSharedPreferenceChanged(SettingsActivity.java:56)
07-03 09:46:22.563: E/AndroidRuntime(421): at android.app.ContextImpl$SharedPreferencesImpl$EditorImpl.commit(ContextImpl.java:2830)
07-03 09:46:22.563: E/AndroidRuntime(421): at com.centauri.equations.settings.SettingsActivity.onSharedPreferenceChanged(SettingsActivity.java:56)
07-03 09:46:22.563: E/AndroidRuntime(421): at android.app.ContextImpl$SharedPreferencesImpl$EditorImpl.commit(ContextImpl.java:2830)
07-03 09:46:22.563: E/AndroidRuntime(421): at com.centauri.equations.settings.SettingsActivity.onSharedPreferenceChanged(SettingsActivity.java:56)
07-03 09:46:22.563: E/AndroidRuntime(421): at android.app.ContextImpl$SharedPreferencesImpl$EditorImpl.commit(ContextImpl.java:2830)
07-03 09:46:22.563: E/AndroidRuntime(421): at com.centauri.equations.settings.SettingsActivity.onSharedPreferenceChanged(SettingsActivity.java:56)
07-03 09:46:22.563: E/AndroidRuntime(421): at android.app.ContextImpl$SharedPreferencesImpl$EditorImpl.commit(ContextImpl.java:2830)
07-03 09:46:22.563: E/AndroidRuntime(421): at com.centauri.equations.settings.SettingsActivity.onSharedPreferenceChanged(SettingsActivity.java:56)
07-03 09:46:22.563: E/AndroidRuntime(421): at android.app.ContextImpl$SharedPreferencesImpl$EditorImpl.commit(ContextImpl.java:2830)
07-03 09:46:22.563: E/AndroidRuntime(421): at com.centauri.equations.settings.SettingsActivity.onSharedPreferenceChanged(SettingsActivity.java:56)
07-03 09:46:22.563: E/AndroidRuntime(421): at android.app.ContextImpl$SharedPreferencesImpl$EditorImpl.commit(ContextImpl.java:2830)
07-03 09:46:22.563: E/AndroidRuntime(421): at com.centauri.equations.settings.SettingsActivity.onSharedPreferenceChanged(SettingsActivity.java:56)
07-03 09:46:22.563: E/AndroidRuntime(421): at android.app.ContextImpl$SharedPreferencesImpl$EditorImpl.commit(ContextImpl.java:2830)
07-03 09:46:22.563: E/AndroidRuntime(421): at com.centauri.equations.settings.SettingsActivity.onSharedPreferenceChanged(SettingsActivity.java:56)
07-03 09:46:22.563: E/AndroidRuntime(421): at android.app.ContextImpl$SharedPreferencesImpl$EditorImpl.commit(ContextImpl.java:2830)
07-03 09:46:22.563: E/AndroidRuntime(421): at com.centauri.equations.settings.SettingsActivity.onSharedPreferenceChanged(SettingsActivity.java:56)
07-03 09:46:22.563: E/AndroidRuntime(421): at android.app.ContextImpl$SharedPreferencesImpl$EditorImpl.commit(ContextImpl.java:2830)
07-03 09:46:22.563: E/AndroidRuntime(421): at com.centauri.equations.settings.SettingsActivity.onSharedPreferenceChanged(SettingsActivity.java:56)
07-03 09:46:22.563: E/AndroidRuntime(421): at android.app.ContextImpl$SharedPreferencesImpl$EditorImpl.commit(ContextImpl.java:2830)
07-03 09:46:22.563: E/AndroidRuntime(421): at com.centauri.equations.settings.SettingsActivity.onSharedPreferenceChanged(SettingsActivity.java:56)
07-03 09:46:22.563: E/AndroidRuntime(421): at android.app.ContextImpl$SharedPreferencesImpl$EditorImpl.commit(ContextImpl.java:2830)
07-03 09:46:22.563: E/AndroidRuntime(421): at com.centauri.equations.settings.SettingsActivity.onSharedPreferenceChanged(SettingsActivity.java:56)
07-03 09:46:22.563: E/AndroidRuntime(421): at android.app.ContextImpl$SharedPreferencesImpl$EditorImpl.commit(ContextImpl.java:2830)
07-03 09:46:22.563: E/AndroidRuntime(421): at com.centauri.equations.settings.SettingsActivity.onSharedPreferenceChanged(SettingsActivity.java:56)
07-03 09:46:22.563: E/AndroidRuntime(421):
當我打電話editor.commit()
哈,我是更快:D –
是的幾秒:( – Caner