Weird exception: Cannot cast String to Boolean when using getBoolean

后端 未结 7 1860
温柔的废话
温柔的废话 2021-01-04 04:44

I\'m getting a very weird error. I have 2 activities. On both I\'m getting the SharedPreferences using MODE_PRIVATE (if it matters) by sp = g

7条回答
  •  清歌不尽
    2021-01-04 05:41

    Use the below code to set the boolean value in 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 SharedPreference use this code:

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

提交回复
热议问题