12

I'm trying to setup a preferences activity but my app keeps crashing and I get the following logcat:

FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.appthing.myapp/com.appthing.myapp.Main}: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer

Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer 07-24 16:37:59.556: E/AndroidRuntime(17384): at android.app.SharedPreferencesImpl.getInt(SharedPreferencesImpl.java:240)

In my Main activity I have the following code inside the onResume() method:

    SeekBar tipSeekBar = (SeekBar) findViewById(R.id.tipSeekBar);
    SeekBar splitSeekBar = (SeekBar) findViewById(R.id.splitSeekBar);

    SharedPreferences preferences = PreferenceManager
            .getDefaultSharedPreferences(this);

    tipSeekBar.setProgress(preferences.getInt("defaultTip", 15));
    splitSeekBar.setProgress(preferences.getInt("defaultSplit", 1));
    tipSeekBar.setMax(preferences.getInt("maxTip", 25));
    splitSeekBar.setMax(preferences.getInt("maxSplit", 10));

Here is what I have in the Preference class (as requested):

addPreferencesFromResource(R.layout.preferences);
// I was told in tutorials this is all I need in the oncreate method

I don't understand why its saying something about a string. All my values are integers and I am using android:inputType="number" to make sure only an int can be entered. I have also tried uninstalling and re-installing the app to clear the cache and nothing works.

RESOLVED:

"Your Preferences in XML, even if you set android:inputType="number" are still stored as a String" (by Waza_Be). All I had to do is do Integer.parseInt() to grab the correct value.

Community
  • 1
  • 1

3 Answers3

25

Your Preferences in XML, even if you set android:inputType="number" are still stored as a String

You have 2 choices:

1) the 'not-so-nice': Integer.parseInt( preferences.getString("defaultTip", "15"));

2) Using your own type of Integer Preference. More complicated to set in first place but really better (similar question here: https://stackoverflow.com/a/3755608/327402)

Community
  • 1
  • 1
Waza_Be
  • 39,407
  • 49
  • 186
  • 260
  • "Your Preferences in XML, even if you set android:inputType="number" are still stored as a String" thats the key, I never knew this! Thank you! –  Jul 24 '13 at 21:14
  • Glad that I could help. – Waza_Be Jul 24 '13 at 21:34
  • 1
    Well, that's quite silly. Especially when your ListPreferences are referencing an , it still treats them as Strings. Thanks for the tip. – phreakhead Nov 07 '15 at 02:23
  • I set string value "" in shared preference for integer object, so when i get value from preference i am getting this issue, so please make sure you set value into preference is integer only. – varotariya vajsi Dec 06 '17 at 14:01
  • 1
    ok, so this is the second time I have run into this, years apart, and I still have to ask - what is the *point* of `preferences.getInt` if it's just going to throw an exception?! – Michael Aug 11 '19 at 19:37
1

The exception can also be thrown if

  • you are using the same key for storing two or more different types of values
  • at first you stored the value as String and then changed storing and retrieving implementation for int value. For this case just clear the cache.
Levon Petrosyan
  • 8,815
  • 8
  • 54
  • 65
-1

If you are storing the preferences with the right data types, then it should not be problem. Please look at the JavaDoc here.

Phani
  • 5,319
  • 6
  • 35
  • 43