访问共享首选项时 Android 中的 ConcurrentModificationException [英] ConcurrentModificationException in Android while accessing Shared Preferences
问题描述
当我开发一个安卓应用程序时,我遇到了我没有任何线索的异常;我用谷歌搜索了相关主题,但没有一个有帮助.
致命异常:java.util.ConcurrentModificationExceptionjava.util.HashMap$HashIterator.nextEntry (HashMap.java:806)java.util.HashMap$KeyIterator.next (HashMap.java:833)com.android.internal.util.XmlUtils.writeSetXml (XmlUtils.java:298)com.android.internal.util.XmlUtils.writeValueXml (XmlUtils.java:447)com.android.internal.util.XmlUtils.writeMapXml (XmlUtils.java:241)com.android.internal.util.XmlUtils.writeMapXml (XmlUtils.java:181)android.app.SharedPreferencesImpl.writeToFile (SharedPreferencesImpl.java:596)android.app.SharedPreferencesImpl.access $800 (SharedPreferencesImpl.java:52)android.app.SharedPreferencesImpl$2.run (SharedPreferencesImpl.java:511)java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1112)java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:587)java.lang.Thread.run (Thread.java:841)
首选项是线程安全的(!),但不是进程安全的.的答案@mohan mishra 根本不正确,不需要同步所有内容.正如另一个问题中所述,这里的问题是,根据文档,您不得修改 getStringSet 和 getAll 返回的任何实例
getStringSet()
请注意,您不得修改此调用返回的集合实例.如果您这样做,则无法保证存储数据的一致性,也不能保证您完全可以修改实例.
getAll()
<块引用>
注意,不能修改此方法返回的集合,或更改其任何内容.您存储的数据的一致性是不保证会这样做.
When I develop an android app, I run into the exception which I do not have any clue; I have googled related topics but none of them helped.
Fatal Exception: java.util.ConcurrentModificationException
java.util.HashMap$HashIterator.nextEntry (HashMap.java:806)
java.util.HashMap$KeyIterator.next (HashMap.java:833)
com.android.internal.util.XmlUtils.writeSetXml (XmlUtils.java:298)
com.android.internal.util.XmlUtils.writeValueXml (XmlUtils.java:447)
com.android.internal.util.XmlUtils.writeMapXml (XmlUtils.java:241)
com.android.internal.util.XmlUtils.writeMapXml (XmlUtils.java:181)
android.app.SharedPreferencesImpl.writeToFile (SharedPreferencesImpl.java:596)
android.app.SharedPreferencesImpl.access$800 (SharedPreferencesImpl.java:52)
android.app.SharedPreferencesImpl$2.run (SharedPreferencesImpl.java:511)
java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1112)
java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:587)
java.lang.Thread.run (Thread.java:841)
Preferences are thread safe(!), but not process safe. The answer of @mohan mishra simply not true, no need to synchronize everything. The problem here, as statet out in another question is, that per documentation you MUST NOT modify any instance that is returned by getStringSet and getAll
getStringSet()
Note that you must not modify the set instance returned by this call. The consistency of the stored data is not guaranteed if you do, nor is your ability to modify the instance at all.
getAll()
Note that you must not modify the collection returned by this method, or alter any of its contents. The consistency of your stored data is not guaranteed if you do.
这篇关于访问共享首选项时 Android 中的 ConcurrentModificationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!