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

它在大多数情况下都是有效的,但是测试人员说有时他们会遇到问题。有时回装根本不起作用。
这是我的课:
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为空。
在方法的开头检查该情况,如果出现错误,则抛出异常,您将从传递此类无效密钥的位置看到:

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

08-04 13:08