User settings saved in SharedPreferences removed or lost between reloads of app

前端 未结 3 1054
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-18 00:37

My app stores simple settings in SharedPreferences it works fine. However for one person who\'s downloaded my app is having problems. The settings in the SharedPreferences a

相关标签:
3条回答
  • 2020-12-18 01:23

    I had same problem. Fortunately, I had access to the device and it helped me to find problem. First of all, I have studied log file and found error:

    W/SharedPreferencesImpl(31354): org.xmlpull.v1.XmlPullParserException: Map value without name attribute: string
    

    So, preferences file was corrupted in some way. I have made preferences file world-accessible in my application:

    SharedPreferences prefs = context.getSharedPreferences("main", Context.MODE_WORLD_READABLE);
    

    Then I pull the file from device to computer

    adb pull data/data/my.package.name/shared_prefs/main.xml c:\main.xml
    

    and check preferences file content:

    <?xml version='1.0' encoding='utf-8' standalone='yes' ?>
    <map>
    <string>Paris,France</string>
    <string name="HideStatusBar">1</string>
    

    First parameter has no "name" attribute. I have checked a code and found that in some circumstances first parameters was written in follow way:

     SharedPreferences.Editor e = _Prefs.edit();
     e.putString(null, paramValue);
     e.commit()     
    

    Name was null. I have fixed the error and problem has disappeared. So, trivial error can completely corrupt preferences file.

    0 讨论(0)
  • 2020-12-18 01:34

    We are experienced same problems with our Android apps. Our userbase is pretty big (several million users) and by our statistics subjected problems occured for about 0,2% - 0,3% of users. It seems to be not so much, but with our userbase it thousands of users.

    After long search for fixes of this problem, we've made a decision to stop using SharedPreferences for our projects. We are using simple SQLiteDatabase instead, and it works very well.

    0 讨论(0)
  • 2020-12-18 01:40

    You should put out an update to your app that temporarily saves, clears, and recreates the preferences file.

    I had a similar situation. Some users had not only their preferences file messed up, but also their SQL database. You can't really ask people to delete and reinstall, they may lose data. But your app can automatically back it up first, remove the corrupted files, and then put it all back.

    0 讨论(0)
提交回复
热议问题