Android Preferences error, "String cannot be cast to int"
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)
The exception can also be thrown if
- you are using the same
key
for storing two or more different types ofvalues
- at first you stored the value as
String
and then changed storing and retrieving implementation forint
value. For this case just clear the cache.