我使用 SharedPreferences,有时我在 logcat 中收到此警告:
08-27 11:46:55.453: W/SharedPreferencesImpl(21824): getSharedPreferences
08-27 11:46:55.453: W/SharedPreferencesImpl(21824): org.xmlpull.v1.XmlPullParserException: Map value without name attribute: string
08-27 11:46:55.453: W/SharedPreferencesImpl(21824): at com.android.internal.util.XmlUtils.readThisMapXml(XmlUtils.java:568)
08-27 11:46:55.453: W/SharedPreferencesImpl(21824): at com.android.internal.util.XmlUtils.readThisValueXml(XmlUtils.java:821)
08-27 11:46:55.453: W/SharedPreferencesImpl(21824): at com.android.internal.util.XmlUtils.readValueXml(XmlUtils.java:755)
08-27 11:46:55.453: W/SharedPreferencesImpl(21824): at com.android.internal.util.XmlUtils.readMapXml(XmlUtils.java:494)
08-27 11:46:55.453: W/SharedPreferencesImpl(21824): at android.app.SharedPreferencesImpl.loadFromDiskLocked(SharedPreferencesImpl.java:113)
08-27 11:46:55.453: W/SharedPreferencesImpl(21824): at android.app.SharedPreferencesImpl.access$000(SharedPreferencesImpl.java:48)
08-27 11:46:55.453: W/SharedPreferencesImpl(21824): at android.app.SharedPreferencesImpl$1.run(SharedPreferencesImpl.java:87)
它适用于大多数情况,但测试人员说有时他们会遇到问题。有时加载回来根本不起作用。这是我的SharedPreffer
课:
public class SharedPreffer {
String PREF_FILE_NAME = "saveFile";
public SharedPreffer(Context context) {
this.context = context;
}
Context context;
public static final String DEFAULT_VALUE = "";
public void savePreferences(String key, int value) {
SharedPreferences sharedPreferences = context.getSharedPreferences(
PREF_FILE_NAME, 0);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, Integer.toString(value));
editor.commit();
}
public void savePreferences(String key, String value) {
SharedPreferences sharedPreferences = context.getSharedPreferences(
PREF_FILE_NAME, 0);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
}
public void savePreferences(String key, Boolean value) {
SharedPreferences sharedPreferences = context.getSharedPreferences(
PREF_FILE_NAME, 0);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, Boolean.toString(value));
editor.commit();
}
public String loadPreferences(String key) {
SharedPreferences sharedPreferences = context.getSharedPreferences(PREF_FILE_NAME, 0);
return sharedPreferences.getString(key, DEFAULT_VALUE);
}
}
有人可以给我一些提示吗?