问题
As the title suggest my problem is that some SharedPreferences I'm using are conflicting while the app operates, for example they seem to just be over writing each overs values, and not using their 'key'.
Additionally once the application closes fully, force close/reboot, the file is lost completely.
Logcat shows these possible hints, but I can't for the life of me get anything to work:
07-22 13:28:13.980: W/SharedPreferencesImpl(7905):org.xmlpull.v1.XmlPullParserException: Map value without name attribute: string
and
07-22 13:28:13.980: W/SharedPreferencesImpl(7905): at android.app.SharedPreferencesImpl.loadFromDiskLocked(SharedPreferencesImpl.java:113)
Here is my code for the SharedPrefs:
public class SharedPrefs
{
public static String PREF_FILE = "HangedRes";
public static String USERSCORE;
public static String EXTRAWORDS;
/////////////////////////////////////////////////////////////////
static SharedPreferences settings;
static SharedPreferences.Editor editor;
///////////////////////////////////////////////////////////////////
public static String getStringPreference(Context context, String key)
{
settings = context.getSharedPreferences(PREF_FILE, Context.MODE_WORLD_READABLE);
editor = settings.edit();
String result = settings.getString(key, null);
return result;
}
public static void setStringPreference(Context context, String key, String value)
{
settings = context.getSharedPreferences(PREF_FILE, Context.MODE_WORLD_READABLE);
editor = settings.edit();
editor.putString(key, value);
//editor.apply();
editor.commit();
}
/*
public static boolean getBooleanPreference(Context context, String key) {
settings = context.getSharedPreferences(PREF_FILE, 0);
editor = settings.edit();
boolean result = settings.getBoolean(key, false);
return result;
}
public static void setBooleanPreference(Context context, String key, boolean value)
{
settings = context.getSharedPreferences(PREF_FILE, 0);
editor = settings.edit();
editor.putBoolean(key, value);
editor.commit();
}
*/
}
Here is how I am calling those methods in my main code:
// Get current user points and display //////////////////////////
userScore = SharedPrefs.getStringPreference(this, SharedPrefs.USERSCORE);
//Check if things are unlocked
ownWords = SharedPrefs.getStringPreference(this, SharedPrefs.EXTRAWORDS);
Any help would be greatly appreciated.
回答1:
Oh!
public static String USERSCORE;
public static String EXTRAWORDS;
where you initialized these keys..?? :)
make it like this
public static String USERSCORE="USERSCORE";
public static String EXTRAWORDS="EXTRAWORDS";`
来源:https://stackoverflow.com/questions/11601109/trouble-with-sharedpreferences-conflicting-during-app-operation-and-then-disappe