我使用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);
}
}
有人能告诉我一些有关此事的提示吗?
答案 0 :(得分:3)
Map value without name attribute: string
表示您的String key
为空或空。
在方法的开头检查这个案例并在出现问题时抛出异常,你会看到你传递这些无效密钥的位置:
if (key == null || key.equals("")) throw new IllegalArgumentException("Key can't be null or empty string");
答案 1 :(得分:0)