我使用SharedPreferences,有時我在logcat中得到了這樣的警告:SharedPreferences奇怪的警告
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);
}
}
任何人都可以通過我這個有什麼祕訣?
'有時他們遇到問題'是一個非常好的問題描述。你能否詳細說明一下? –
也許您正在嘗試編寫空名稱「Map name without name attribute」 –
您是否檢查了這兩個鏈接http://stackoverflow.com/questions/5368442/android-getsharedpreferences-error-map-value-without- name-attribute-boolean和http://stackoverflow.com/questions/11601109/trouble-with-sharedpreferences-conflicting-during-app-operation-and-then-disappe – surhidamatya