gpt4 book ai didi

android - SharedPreferences 奇怪的警告

转载 作者:太空狗 更新时间:2023-10-29 12:45:47 24 4
gpt4 key购买 nike

我使用 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);

}

}

谁能给我一些关于这方面的提示?

最佳答案

Map value without name attribute: string 表明您的 String key 为 null 或空。

在方法的开头检查这种情况,如果出现问题则抛出异常,您将从哪里看到传递此类无效键的位置:

if (key == null || key.equals("")) throw new IllegalArgumentException("Key can't be null or empty string");

关于android - SharedPreferences 奇怪的警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18462170/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com