Leebeedev
Leebeedev

Reputation: 2146

ClassCastException without any specific code error

I'm retrieving an integer value from the sharedpreferences like this:

AudioManager Am = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
    int errorThisLine250 = prefs.getInt("adanvlm", Am.getStreamMaxVolume(AudioManager.STREAM_MUSIC));

and I'm getting this error !!!

java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
        at android.app.SharedPreferencesImpl.getInt(SharedPreferencesImpl.java:239)
        at aim.salatuna7.dialogs.AdanDialog.onCreateDialog(AdanDialog.java:250)

The starnge here is that the app was works great without any errors, today it starts showing this error whenever I show the dialog. The error is at the int errorThisLine250. Any suggestions?

Edit: I tried:

Integer.parseInt(prefs.getString("adanvlm", String.valueOf(Am.getStreamMaxVolume(AudioManager.STREAM_MUSIC))));

and it is NOT working too.

Upvotes: 0

Views: 594

Answers (2)

petey
petey

Reputation: 17140

Looks like your EditTextPreference has a default value that is string:

<EditTextPreference android:defaultValue="0" android:key="adanvlm" />

So, it is probable but a bit odd, that you many continue along using the string version and convert that string to an int before using it in your dialog and elsewhere where an int is needed.

// get the preference value as a string
String adanvlmString = prefs.getString("adanvlm", String.valueOf(Am.getStreamMaxVolume(AudioManager.STREAM_MUSIC)));
// convert the preference value from string to int
int adanvlmInt = Integer.parseInt(adanvlmString);

Or you could subclass EditTextPreference as follows here : PreferenceActivity: save value as integer

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006604

Apparently, tou originally had this SharedPreference as a string preference, then changed it to be an integer preference.

Uninstall and reinstall your app, thereby deleting the contents of internal storage and getting rid of the old edition of your preferences.

Upvotes: 1

Related Questions