title: Android App Development, Shared Preferences getBoolean Problems
body: <p>I am developing an Android App and I am using a switch for the user to designate themselves as Male or Female.</p>
  
  <p>I would like to use sharedPreferences to save the values they enter. I have successfully done this with simple buttons and text, however I cause the app to crash when I try to do this to the switch. </p>
  
  <p>The line that cause the crash is <code>sharedPreferences.getBoolean("gender",false)</code></p>
  
  <p>here is the code (this is my load section):</p>
  
  <pre><code>private void loadSavedPreferences() {
      SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
      String firstnameString = sharedPreferences.getString("firstname", "FirstName");
      FirstName.setText(firstnameString);
      String lastnameString = sharedPreferences.getString("lastname","LastName");
      LastName.setText(lastnameString);
      String mailString = sharedPreferences.getString("email","Email");
      Email.setText(mailString);
      String postalcodeString = sharedPreferences.getString("postalcode","PostalCode");
      PostalCode.setText(postalcodeString);
      Boolean genderBoolean;
      if (sharedPreferences.getBoolean("gender", false)) {
          genderBoolean = true;
      }
      else {
          genderBoolean = false;
      }
      genderSwitch.setChecked(genderBoolean);
  }
  </code></pre>
  
  <p>Every other <code>sharedPreferences.getString()</code> works successfully.</p>
  
  <p>Thanks,</p>
  
  <p>Larry</p>
  
  <p>Here is the logcat after I changed all that code to one line:</p>
  
  <p><code>genderSwitch.setChecked(sharedPreferences.getBoolean("gender", false));</code></p>
  
  <pre><code>java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mobiquitynetworks.www.mobiquitymall/com.mobiquitynetworks.www.mobiquitymall.preferences_page}: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean
              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2334)
              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392)
              at android.app.ActivityThread.access$900(ActivityThread.java:169)
              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
              at android.os.Handler.dispatchMessage(Handler.java:102)
              at android.os.Looper.loop(Looper.java:146)
              at android.app.ActivityThread.main(ActivityThread.java:5487)
              at java.lang.reflect.Method.invokeNative(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:515)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
              at dalvik.system.NativeStart.main(Native Method)
       Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean
              at android.app.SharedPreferencesImpl.getBoolean(SharedPreferencesImpl.java:261)
              at com.mobiquitynetworks.www.mobiquitymall.preferences_page.loadSavedPreferences(preferences_page.java:55)
              at com.mobiquitynetworks.www.mobiquitymall.preferences_page.onCreate(preferences_page.java:42)
              at android.app.Activity.performCreate(Activity.java:5451)
              at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392)
              at android.app.ActivityThread.access$900(ActivityThread.java:169)
              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
              at android.os.Handler.dispatchMessage(Handler.java:102)
              at android.os.Looper.loop(Looper.java:146)
              at android.app.ActivityThread.main(ActivityThread.java:5487)
              at java.lang.reflect.Method.invokeNative(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:515)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
              at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>Here is how I save the boolean value using EDITOR:</p>
  
  <pre><code>private void savePreferencesB(String key, Boolean value) {
          SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
          Editor editor = sharedPreferences.edit();
          editor.putBoolean(key, value);
          editor.apply();
      }
  </code></pre>