Marc Rasmussen Marc Rasmussen - 6 months ago 14
Java Question

Preference cannot cast java.lang.boolean to String

I have the following Preference class:

public class AppPreferencesActivity extends PreferenceActivity {
private SharedPreferences appPrefs;
private SharedPreferences.Editor prefEditor;
private Mediator mediator;
private SharedPreferences.OnSharedPreferenceChangeListener listener;
private User user;
@SuppressWarnings("deprecation")
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//---load the preferences from an XML file---
addPreferencesFromResource(R.xml.preferences);
this.appPrefs = PreferenceManager.getDefaultSharedPreferences(this);
this.mediator = (Mediator) getApplication();
this.prefEditor = appPrefs.edit();
this.user = mediator.getUser();
setPreferences();
listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
// Implementation
updatePreference(key);
Toast.makeText(AppPreferencesActivity.this,
"Information updated",
Toast.LENGTH_SHORT).show();
}
};
appPrefs.registerOnSharedPreferenceChangeListener(listener);

}
private void setPreferences(){
prefEditor.putString("nameEdit", user.getName() + " "+user.getLastName());
prefEditor.putString("streetEdit", user.getStreetName());
prefEditor.putString("streetNumberEdit", ""+user.getStreetNr());
prefEditor.putString("emailEdit", user.getEmail());
prefEditor.commit();
preferenceAddSummary();
}
public boolean checkEmailStatus(){
return appPrefs.contains("emailEdit");
}
@SuppressWarnings("deprecation")
private void preferenceAddSummary() {
for (Entry<String, ?> key : appPrefs.getAll().entrySet()) {
findPreference(key.getKey()).setSummary(appPrefs.getString(key.getKey(), "Not yet entered"));
// this is where im getting the error

}


However when i run this i get the following


Cannot cast Java.lang.boolean to Java.lang.String


I get this error at the loop im calling to setSumary can anyone tell me why this is happening?

UPDATE

Full stackTrace in text:

05-02 02:28:15.835: E/AndroidRuntime(1775): FATAL EXCEPTION: main
05-02 02:28:15.835: E/AndroidRuntime(1775): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.konkurrencesigner/com.example.konkurrencesigner.AppPreferencesActivity}: java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.String
05-02 02:28:15.835: E/AndroidRuntime(1775): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-02 02:28:15.835: E/AndroidRuntime(1775): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-02 02:28:15.835: E/AndroidRuntime(1775): at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-02 02:28:15.835: E/AndroidRuntime(1775): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-02 02:28:15.835: E/AndroidRuntime(1775): at android.os.Handler.dispatchMessage(Handler.java:99)
05-02 02:28:15.835: E/AndroidRuntime(1775): at android.os.Looper.loop(Looper.java:137)
05-02 02:28:15.835: E/AndroidRuntime(1775): at android.app.ActivityThread.main(ActivityThread.java:5039)
05-02 02:28:15.835: E/AndroidRuntime(1775): at java.lang.reflect.Method.invokeNative(Native Method)
05-02 02:28:15.835: E/AndroidRuntime(1775): at java.lang.reflect.Method.invoke(Method.java:511)
05-02 02:28:15.835: E/AndroidRuntime(1775): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-02 02:28:15.835: E/AndroidRuntime(1775): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-02 02:28:15.835: E/AndroidRuntime(1775): at dalvik.system.NativeStart.main(Native Method)
05-02 02:28:15.835: E/AndroidRuntime(1775): Caused by: java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.String
05-02 02:28:15.835: E/AndroidRuntime(1775): at android.app.SharedPreferencesImpl.getString(SharedPreferencesImpl.java:224)
05-02 02:28:15.835: E/AndroidRuntime(1775): at com.example.konkurrencesigner.AppPreferencesActivity.preferenceAddSummary(AppPreferencesActivity.java:55)
05-02 02:28:15.835: E/AndroidRuntime(1775): at com.example.konkurrencesigner.AppPreferencesActivity.setPreferences(AppPreferencesActivity.java:47)
05-02 02:28:15.835: E/AndroidRuntime(1775): at com.example.konkurrencesigner.AppPreferencesActivity.onCreate(AppPreferencesActivity.java:28)
05-02 02:28:15.835: E/AndroidRuntime(1775): at android.app.Activity.performCreate(Activity.java:5104)
05-02 02:28:15.835: E/AndroidRuntime(1775): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-02 02:28:15.835: E/AndroidRuntime(1775): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-02 02:28:15.835: E/AndroidRuntime(1775): ... 11 more

Answer

It obviously that some prefs are checked and only return boolean value. So you cannot use this directly.

 for (Entry<String, ?> key : appPrefs.getAll().entrySet()) {
            findPreference(key.getKey()).setSummary(appPrefs.getString(key.getKey(), "Not yet entered"));

I think maybe you should use like that:

 for (Entry<String, ?> key : appPrefs.getAll().entrySet()) {
            Object result = key.getValue();
            if(result instanceof Boolean) {
               //handle boolean
            } else if(result instanceof String) {
                //handle String
            }
            findPreference(key.getKey()).setSummary(result));
 }