我正在开发Android应用程序,我正在使用一个开关让用户将自己指定为男性或女性。
我想使用sharedPreferences来保存他们输入的值。我已经使用简单的按钮和文本成功完成了此操作,但是当我尝试对交换机执行此操作时,我会导致应用程序崩溃。
导致崩溃的行是sharedPreferences.getBoolean("gender",false)
这是代码(这是我的加载部分):
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);
}
其他sharedPreferences.getString()
成功运作。
谢谢,
拉里
这是我将所有代码更改为一行后的logcat:
genderSwitch.setChecked(sharedPreferences.getBoolean("gender", false));
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)
编辑:
以下是使用EDITOR保存布尔值的方法:
private void savePreferencesB(String key, Boolean value) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
Editor editor = sharedPreferences.edit();
editor.putBoolean(key, value);
editor.apply();
}
答案 0 :(得分:1)
我从logcat中了解到,您正在尝试检索"性别"作为布尔值,但它存储了一个字符串。
所以它抛出一个异常,查看log cat。
引起:java.lang.ClassCastException:java.lang.String无法强制转换为java.lang.Boolean 在android.app.SharedPreferencesImpl.getBoolean(SharedPreferencesImpl.java:261)
尝试这个来检查这样的项是否退出,然后检索并将字符串转换为boolean:
Boolean genderBoolean;
if (sharedPreferences.contains("gender")) {
String temp = sharedPreference.getString("gender", "false");
genderBoolean = Boolean.parseBoolean(temp);
}
else {
genderBoolean = false;
}
现在,我会建议您在共享偏好中保存价值的部分内容,您可以这样做:
sharedPreference.edit().putBoolean("locked", true).commit();
在检索部分,请使用:
if (sharedPreferences.contains("gender")) {
genderBoolean = sharedPreferences.getBoolean("gender", false)
}
else {
genderBoolean = false;
}
但是,如果您不想修改任何内容,请使用第一个选项,但我建议您采取第二种方式。