1

In my app, I'm using getPreferences(MODE_PRIVATE) with putString(), putBoolean(), etc. to save app state. 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. 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. Any ideas on what's going on and how to fix this? Many thanks.

Charles
  • 50,943
  • 13
  • 104
  • 142
Aleks G
  • 56,435
  • 29
  • 168
  • 265

2 Answers2

1

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.

Aleks G
  • 56,435
  • 29
  • 168
  • 265
-1

Retry after factory-resetting the device. I had a similar issue, and this worked for me

Perception
  • 79,279
  • 19
  • 185
  • 195
  • Thanks, but this isn't the option. I see this happening occasionally on an emulator and on two different handsets. I can't tell customers that have problems with the app to "reset your device to factory". This isn't a good advice, sorry. – Aleks G Jun 21 '12 at 08:11