2
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()

4

2 回答 2

5

您正在调用commit()您的onSharedPreferenceChanged()- 方法,当有人调用时调用该方法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的文章:

在更改、添加或删除共享首选项时调用。即使将首选项设置为其现有值,也可能会调用它。

因此,每次调用commit()-objectSharedPreferences时。

于 2012-07-03T13:57:44.390 回答
3

onSharedPreferenceChanged提交时将被调用。但是在里面onSharedPreferenceChangedcommit再次调用所以它会导致无限循环。

于 2012-07-03T13:58:14.563 回答