gpt4 book ai didi

android - 提交 SharedPreferences 时发生 StackOverFlowException

转载 作者:太空狗 更新时间:2023-10-29 15:55:36 25 4
gpt4 key购买 nike

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;
}
}

我明白了:

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() 时出现此错误

最佳答案

您在 onSharedPreferenceChanged() 方法中调用 commit(),当有人调用 commit() 时调用该方法。这是一个无限循环。

这可以在堆栈跟踪中看到(查看行号):

at ...$EditorImpl.commit(ContextImpl.java:2822)
at ...SettingsActivity.onSharedPreferenceChanged(SettingsActivity.java:56)
at ...$EditorImpl.commit(ContextImpl.java:2822)
at ...SettingsActivity.onSharedPreferenceChanged(SettingsActivity.java:56)
at ...$EditorImpl.commit(ContextImpl.java:2822)
at ...SettingsActivity.onSharedPreferenceChanged(SettingsActivity.java:56)
[...]

这是来自 Android Docs 的文章:

Called when a shared preference is changed, added, or removed. This may be called even if a preference is set to its existing value.

因此,每次您在 SharedPreferences 对象上调用 commit() 时。

关于android - 提交 SharedPreferences 时发生 StackOverFlowException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11312478/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com