SharedPreferences - java.lang.Boolean 不能转换为 java.lang.String [英] SharedPreferences - java.lang.Boolean cannot be cast to java.lang.String

查看:100
本文介绍了SharedPreferences - java.lang.Boolean 不能转换为 java.lang.String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对不应该抛出的异常感到困惑.错误如下:

I'm confused with an exception that is thrown for something that shouldn't. The error is the following:

java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.String

但是,我的代码不会尝试将布尔值转换为 String Ô_o编译器是否可能错误地指示异常来自哪里?我用的是华为

But, my code doesn't try to cast a Boolean to String Ô_o Is it possible that the compiler is wrong in indicating where the exception is coming from? I'm using an Huawei

这部分触发异常:

if(SharedPreferencesManager.getUserMRZ(ScanEUID.this).equals("0")
&& SharedPreferencesManager.getUserDocumentIdFront(ScanEUID.this) != null 
&& SharedPreferencesManager.getUserDocumentIdBack(ScanEUID.this) != null)
{

相关方法如下

public static String getUserMRZ(Context context){
    return getSharedPreferences(context).getString(Constants.USER_HAS_MRZ, "0");
}
public static String getUserDocumentIdFront(Context context) {
    return getSharedPreferences(context).getString(Constants.USER_DOCUMENT_ID_FRONT, null);
}
public static String getUserDocumentIdBack(Context context) {
    return getSharedPreferences(context).getString(Constants.USER_DOCUMENT_ID_BACK, null);
}

setMRZ 方法

public static void setUserMRZ(Context context, String has_mrz){
        final SharedPreferences.Editor editor = getSharedPreferences(context).edit();
        editor.putString(Constants.USER_HAS_MRZ, has_mrz);
        editor.apply();
}

推荐答案

检查你的 setter,我刚刚遇到了同样的问题,我发现我错误地做了一个错误的复制/面食,并在一个布尔值的 setter...

Check your setters, I just had the same issue and I discovered that I'd mistakenly done a bad copy/pasta and used the Key for that value in a setter for a Boolean value...

这篇关于SharedPreferences - java.lang.Boolean 不能转换为 java.lang.String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆