java 奇怪的异常:使用 getBoolean 时无法将 String 转换为 Boolean

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12851626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 10:28:47  来源:igfitidea点击:

Weird exception: Cannot cast String to Boolean when using getBoolean

javaandroidexceptioncastingsharedpreferences

提问by La bla bla

I'm getting a very weird error. I have 2 activities. On both I'm getting the SharedPreferencesusing MODE_PRIVATE(if it matters) by sp = getPreferences(MODE_PRIVATE);on each activity's onCreate()I'm calling sp.getBoolean(IntroActivity.SHOW_INTRO, true)

我收到一个非常奇怪的错误。我有2个活动。在 我正在调用的每个活动上,我都在SharedPreferences使用MODE_PRIVATE(如果重要的话)sp = getPreferences(MODE_PRIVATE);onCreate()sp.getBoolean(IntroActivity.SHOW_INTRO, true)

On the IntroActivitythis works fine. But when I'm trying in the main activity, I'm getting this

IntroActivity这工作正常。但是当我尝试主要活动时,我得到了这个

10-12 04:55:23.208: E/AndroidRuntime(11668): FATAL EXCEPTION: main
10-12 04:55:23.208: E/AndroidRuntime(11668): java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean
10-12 04:55:23.208: E/AndroidRuntime(11668):    at android.app.SharedPreferencesImpl.getBoolean(SharedPreferencesImpl.java:242)
10-12 04:55:23.208: E/AndroidRuntime(11668):    at com.lablabla.parkme.ParkMeActivity.onClick(ParkMeActivity.java:194)
10-12 04:55:23.208: E/AndroidRuntime(11668):    at android.view.View.performClick(View.java:4084)
10-12 04:55:23.208: E/AndroidRuntime(11668):    at android.view.View$PerformClick.run(View.java:16966)
10-12 04:55:23.208: E/AndroidRuntime(11668):    at android.os.Handler.handleCallback(Handler.java:615)
10-12 04:55:23.208: E/AndroidRuntime(11668):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-12 04:55:23.208: E/AndroidRuntime(11668):    at android.os.Looper.loop(Looper.java:137)
10-12 04:55:23.208: E/AndroidRuntime(11668):    at android.app.ActivityThread.main(ActivityThread.java:4745)
10-12 04:55:23.208: E/AndroidRuntime(11668):    at java.lang.reflect.Method.invokeNative(Native Method)
10-12 04:55:23.208: E/AndroidRuntime(11668):    at java.lang.reflect.Method.invoke(Method.java:511)
10-12 04:55:23.208: E/AndroidRuntime(11668):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-12 04:55:23.208: E/AndroidRuntime(11668):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-12 04:55:23.208: E/AndroidRuntime(11668):    at dalvik.system.NativeStart.main(Native Method)

I made sure that I'm not putting a Stringsomewhere in the middle with that same key

我确保我没有String用相同的键在中间放置一个

Any ideas?

有任何想法吗?

Thanks!

谢谢!

EDIT: some code:

编辑:一些代码:

//onCreate()
sp = getPreferences(MODE_PRIVATE);

// other method
boolean showIntro = sp.getBoolean(IntroActivity.SHOW_INTRO, true); // Exception is here
showIntroCheckBox.setChecked(showIntro);

If it matters, the code which throws the exception is inside a button's onClick

如果重要的话,抛出异常的代码在按钮的内部 onClick

回答by Geobits

If there's ever been a string with that key, even if by accident, it will stay there until you clear the app's data or uninstall. Try uninstalling it to see if it still occurs.

如果曾经有一个带有该键的字符串,即使是偶然的,它也会保留在那里,直到您清除应用程序的数据或卸载。尝试卸载它,看看它是否仍然存在。

回答by Sam

The exception occurs in this Android method:

这个Android方法发生了异常:

public boolean getBoolean(String key, boolean defValue) {
    synchronized (this) {
        awaitLoadedLocked();
        Boolean v = (Boolean)mMap.get(key); // On this line
        return v != null ? v : defValue;
    }
}

The only sense I can make of this error is that your are reusing the key IntroActivity.SHOW_INTROfor a String somewhere else in your code.

我对这个错误的唯一理解是你IntroActivity.SHOW_INTRO在代码中的其他地方重用了 String的键。

回答by Nishant

Use the below code to set the boolean value in SharedPreference:

使用以下代码在 中设置布尔值SharedPreference

    SharedPreferences appSharedPrefs = PreferenceManager
            .getDefaultSharedPreferences(this.getApplicationContext());
    Editor prefsEditor = appSharedPrefs.edit();
    prefsEditor.putBoolean(IntroActivity.SHOW_INTRO, true);
    prefsEditor.commit();

And to retrieve the boolean value from SharedPreferenceuse this code:

SharedPreference使用以下代码检索布尔值:

SharedPreferences appSharedPrefs = PreferenceManager
            .getDefaultSharedPreferences(this.getApplicationContext());
    boolean showIntro = appSharedPrefs.getBoolean(IntroActivity.SHOW_INTRO, true);

回答by BBdev

I am assuming that

我假设

sp.getBoolean(IntroActivity.SHOW_INTRO, true)// this line returns a String value.

so you can do like this

所以你可以这样做

boolean showIntro = Boolean.parseBoolean(sp.getBoolean(IntroActivity.SHOW_INTRO, true));

回答by Anil Jadhav

The line sp.getBoolean(IntroActivity.SHOW_INTRO, true)// this line returns a String value.

sp.getBoolean(IntroActivity.SHOW_INTRO, true)// 这一行返回一个字符串值。

so you have to do as given below,

所以你必须按照下面给出的做,

String flag=sp.getBoolean(IntroActivity.SHOW_INTRO, true);

if(flag.equalsIgnoreCase("true")){
    boolean showIntro = true;   
}else{
    boolean showIntro = false;
}

Try this it will definitely works.

试试这个它肯定会起作用。

回答by Abhilash

This is the common mistake which everyone will make with the key , I guess IntroActivity.SHOW_INTRO="xyz_key" if , You use the same "xyz_key" for another Constant this error will occur so the value of key should be unique.. Happy coding :)

这是每个人都会在使用 key 时犯的常见错误,我猜 IntroActivity.SHOW_INTRO="xyz_key" 如果,你对另一个常量使用相同的“xyz_key”会发生这个错误,所以 key 的值应该是唯一的..快乐编码:)

回答by hzitoun

Got this exception when, by mistake, I've given to two preferences the samekeyin the XML file android:key="your_key"!

当我错误地在 XML 文件中为两个首选项提供了相同的时,出现了这个异常android:key="your_key"

So double checking your settings.xml may help.

因此,仔细检查您的 settings.xml 可能会有所帮助。