Java Question

Android: When I try to access Boolean SharedPreference, it returns error about casting string to boolean


AGAIN: the line 36 is: ToPushVar = mPrefs.getBoolean("PushNotifications", true);


I started creating a simple rss reader today. I want to store the settings in Shared Preferences. Here is my saving code (with alertdialogs for debugging):

public void toSave(View v) {
SharedPreferences prefs = getSharedPreferences(
"bdapps.ujsag_frisshirekatelefonodon", Context.MODE_PRIVATE);
String NewsPaperSetting;
boolean ToPush = ((Switch)findViewById(R.id.pushNotifications)).isChecked();
boolean Newspaper =( (Switch)findViewById(R.id.integratedReader)).isChecked();
SharedPreferences.Editor mEditor = prefs.edit();
mEditor.clear();
mEditor.putBoolean("PushNotifications", ToPush);
mEditor.putBoolean("UseReader", Newspaper);
mEditor.apply();
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Változók értékei:");
alertDialog.setMessage("Mentés:" + ToPush + Newspaper);
alertDialog.show();
SharedPreferences mPrefs = getSharedPreferences("bdapps.ujsag_frisshirekatelefonodon", 0);
boolean SendNotifications, UseBDReader;
Boolean ToPushVar = mPrefs.getBoolean("PushNotifications", false);
Boolean abc = mPrefs.getBoolean("PushNotifications", false);
AlertDialog dia = new AlertDialog.Builder(this).create();
dia.setTitle("Változók értékei:");
dia.setMessage("Üzenetek küldése:" +ToPushVar + " Olvasó: " + abc );
dia.show();
}


The debug states it successfully saved the variable with either true or false value, but when I restart the application, it gives this error:


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


My method of reading the values:

public void RetrieveValues() {
boolean ToPushVar;
boolean Reader;

SharedPreferences mPrefs = getSharedPreferences("bdapps.ujsag_frisshirekatelefonodon", 0);
ToPushVar = mPrefs.getBoolean("PushNotifications", true); /* Here I got the error*/
Reader = mPrefs.getBoolean("UseReader", false);
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Változók értékei:");
alertDialog.setMessage("Üzenetek küldése:" + ToPushVar + " Olvasó: " + Reader);
alertDialog.show();

}


Full stacktrace:

8-24 10:04:04.068 6476-6476/bdapps.ujsag_frisshirekatelefonodon E/AndroidRuntime: FATAL EXCEPTION: main
Process: bdapps.ujsag_frisshirekatelefonodon, PID: 6476
java.lang.RuntimeException: Unable to start activity ComponentInfo{bdapps.ujsag_frisshirekatelefonodon/bdapps.ujsag_frisshirekatelefonodon.MainActivity}: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2699)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2773)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1434)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5930)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean
at android.app.SharedPreferencesImpl.getBoolean(SharedPreferencesImpl.java:260)
at bdapps.ujsag_frisshirekatelefonodon.MainActivity.RetrieveValues(MainActivity.java:36) at bdapps.ujsag_frisshirekatelefonodon.MainActivity.onCreate(MainActivity.java:76) at android.app.Activity.performCreate(Activity.java:6178)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2652)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2773) 
at android.app.ActivityThread.access$900(ActivityThread.java:177) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1434) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5930) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200) 



I found out the solution. The error occurred, because there was a value of other data type(String) left in. I needed to reinstall the app to fix it. Thanks DsD!

Answer Source

As per android developer link getBoolean throws ClassCastException if there is a preference with this name that is not a boolean. So you can check by changing your key that is "PushNotifications" or you can uninstall the app and install again should work.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download