简体   繁体   English

尝试从Android中的首选项读取字符串时出现XML错误

[英]XML error when trying to read string from preferences in android

In my app, I'm using getPreferences(MODE_PRIVATE) with putString() , putBoolean() , etc. to save app state. 在我的应用程序中,我将getPreferences(MODE_PRIVATE)getPreferences(MODE_PRIVATE) putString()putBoolean()等一起使用以保存应用程序状态。 The amount of data is not great - possible a few strings and a couple of boolean values. 数据量不是很大-可能有几个字符串和几个布尔值。 I never get any errors when saving the data, however when I read it, sometimes I get XML errors. 保存数据时我从来没有遇到任何错误,但是当我读取数据时,有时会出现XML错误。 For example, I'm trying to read the the values with this code: 例如,我正在尝试使用以下代码读取值:

String cr = getPreferences(MODE_PRIVATE).getString("user-input-" + block.getID(), "");

Sometimes this works just fine and on other occasions it produces this error: 有时,这工作正常,在其他情况下,会产生此错误:

03-22 12:42:25.846: W/ApplicationContext(406): getSharedPreferences
03-22 12:42:25.846: W/ApplicationContext(406): org.xmlpull.v1.XmlPullParserException: Error parsing document. (position:line -1, column -1) caused by: org.apache.harmony.xml.ExpatParser$ParseException: At line 3, column 29: not well-formed (invalid token)
03-22 12:42:25.846: W/ApplicationContext(406):  at org.apache.harmony.xml.ExpatPullParser$ByteDocument.flush(ExpatPullParser.java:959)
03-22 12:42:25.846: W/ApplicationContext(406):  at org.apache.harmony.xml.ExpatPullParser$Document.pump(ExpatPullParser.java:770)
03-22 12:42:25.846: W/ApplicationContext(406):  at org.apache.harmony.xml.ExpatPullParser$Document.dequeue(ExpatPullParser.java:814)
03-22 12:42:25.846: W/ApplicationContext(406):  at org.apache.harmony.xml.ExpatPullParser.next(ExpatPullParser.java:303)
03-22 12:42:25.846: W/ApplicationContext(406):  at com.android.internal.util.XmlUtils.readValueXml(XmlUtils.java:674)
03-22 12:42:25.846: W/ApplicationContext(406):  at com.android.internal.util.XmlUtils.readMapXml(XmlUtils.java:469)
03-22 12:42:25.846: W/ApplicationContext(406):  at android.app.ApplicationContext.getSharedPreferences(ApplicationContext.java:340)
03-22 12:42:25.846: W/ApplicationContext(406):  at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:146)
03-22 12:42:25.846: W/ApplicationContext(406):  at android.app.Activity.getPreferences(Activity.java:3444)
03-22 12:42:25.846: W/ApplicationContext(406):  at com.mypackage.BlockActivity$SetupTask.doInBackground(BlockActivity.java:79)
03-22 12:42:25.846: W/ApplicationContext(406):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
03-22 12:42:25.846: W/ApplicationContext(406):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-22 12:42:25.846: W/ApplicationContext(406):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-22 12:42:25.846: W/ApplicationContext(406):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
03-22 12:42:25.846: W/ApplicationContext(406):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
03-22 12:42:25.846: W/ApplicationContext(406):  at java.lang.Thread.run(Thread.java:1096)

Line 79 in my code is the one shown above. 我的代码中的第79行是上面显示的那行。 Any ideas on what's going on and how to fix this? 关于正在发生的事情以及如何解决此问题的任何想法? Many thanks. 非常感谢。

After struggling with this for a while, getting this error on both emulator (different versions) and two different handsets, I backed away from saving data into shared preferences. 经过一段时间的努力之后,在模拟器(不同版本)和两个不同的手机上都遇到了这个错误,我放弃了将数据保存到共享首选项中的工作。 I still keep the boolean and integer values in shared prefs, but save string data into a file. 我仍然将布尔值和整数值保留在共享首选项中,但是将字符串数据保存到文件中。 This works every time and never game me any problems. 每次都可以使用,永远不会给我带来任何问题。

One thing of note is that some of the strings may be very long, possibly a few thousand chars. 需要注意的一件事是,有些字符串可能很长,可能是几千个字符。 Maybe it had something to do with the issue. 也许与这个问题有关。

Retry after factory-resetting the device. 将设备恢复出厂设置后重试。 I had a similar issue, and this worked for me 我有一个类似的问题,这对我有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM