Thursday, May 2, 2013

[android help] 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 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


Here is a print of my stacktrace:


enter image description here



.

stackoverflow.comm

1 comment:

  1. Im a novice myself but just solved this issue.

    Preferences.get() returns your Map, but BOTH the key AND value get returned as a STRING, NOT a boolean.

    I just did

    boolean value=false;
    if(pref.get(key).equals("true")) {
    value=true;
    }

    ReplyDelete

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...