2012-07-03 65 views
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; 
    } 
} 

,當我得到這個: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()

回答

5

你在你的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片:

時調用共享偏好改變,添加或刪除。即使首選項設置爲其現有值,也可以調用此 。

錯誤,每當您撥打commit()SharedPreferences -object。

3

onSharedPreferenceChanged將在您提交時調用。但在onSharedPreferenceChanged內部,您再次致電commit,因此導致無限循環。

+1

哈,我是更快:D –

+0

是的幾秒:( – Caner