Adam Varhegyi
Adam Varhegyi

Reputation: 9894

SharedPreferences strange warning

I use SharedPreferences and sometimes I got this warning in 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)

It is working on most cases but testers said sometimes they got problem with it. Sometimes the loading back simply not works. This is my SharedPreffer Class:

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);

    }

}

Can anybody pass me some tips about this ?

Upvotes: 3

Views: 1267

Answers (2)

W I Z A R D
W I Z A R D

Reputation: 1224

I got the Same Warning many times.

Whenever i use shared preference. Later i noticed that this warning will be shown whenever the app crashes with any runtime exceptions. And restarting the Previous Activity Where you are using Shared Preference to get the preference values. Then i Googled it and got this Result from this SO Question

Upvotes: 0

Adam Stelmaszczyk
Adam Stelmaszczyk

Reputation: 19837

Map value without name attribute: string suggests that your String key is null or empty.

Check that case in the beginning of your methods and throw exception if something is wrong, you will see from where you are passing such invalid keys:

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

Upvotes: 3

Related Questions