Android:SharedPreferencesImpl-load异常,SharedPreferences崩溃

时间:2016-06-21 04:40:27

标签: android crash sharedpreferences fatal-error

我不确定我的应用程序崩溃的时间点,我已将应用程序状态存储在SharedPreferences中,如 isUserLoggedIn userId ,但未在SharedPreferences中存储任何冗长的数据。< / p>

崩溃日志:

FATAL EXCEPTION: SharedPreferencesImpl-load
java.lang.ArrayIndexOutOfBoundsException: src.length=8192 srcPos=1 dst.length=8192 dstPos=0 length=-1
    at java.lang.System.arraycopy(Native Method)
    at org.kxml2.io.KXmlParser.fillBuffer(KXmlParser.java:1489)
    at org.kxml2.io.KXmlParser.skip(KXmlParser.java:1574)
    at org.kxml2.io.KXmlParser.parseStartTag(KXmlParser.java:1049)
    at org.kxml2.io.KXmlParser.next(KXmlParser.java:369)
    at org.kxml2.io.KXmlParser.next(KXmlParser.java:310)
    at com.android.internal.util.XmlUtils.readThisMapXml(XmlUtils.java:574)
    at com.android.internal.util.XmlUtils.readThisValueXml(XmlUtils.java:817)
    at com.android.internal.util.XmlUtils.readValueXml(XmlUtils.java:751)
    at com.android.internal.util.XmlUtils.readMapXml(XmlUtils.java:490)
    at android.app.SharedPreferencesImpl.loadFromDiskLocked(SharedPreferencesImpl.java:120)
    at android.app.SharedPreferencesImpl.access$000(SharedPreferencesImpl.java:52)
    at android.app.SharedPreferencesImpl$1.run(SharedPreferencesImpl.java:91)

修改1:

private void setUserStates(boolean isUserLoggedIn, int userId){
        SharedPreferences.Editor editor = getSharedPreferences("My_App", MODE_PRIVATE).edit();
        editor.putBoolean("isUserLoggedIn", isUserLoggedIn);
        editor.putInt("userId", userId);
        editor.commit();
    }

    private boolean getIsUserLoggedIn(){
        SharedPreferences prefs = getSharedPreferences("My_App", MODE_PRIVATE);
        return  prefs.getBoolean("isUserLoggedIn", false);
    }

    private int getUserId(){
        SharedPreferences prefs = getSharedPreferences("My_App", MODE_PRIVATE);
        return  prefs.getInt("userId", 0);
    }

请分享您的想法。

0 个答案:

没有答案