SharedPreferences.getInt() results in ClassCastException - Why?

…衆ロ難τιáo~ 提交于 2019-11-30 17:11:39
Alex Orlov

You can store preferences as sharedPreferences.edit().putInt(..).commit() (as an example);

And then get them as getInt. But if you use EditTextPreference it will set the type of the preference to string. So if you use EditTextPreference to store some data, use Integer.valueOf(getString) to get it back.

If, you put it manually, use getInt().

As a workaround, you can set onPreferenceChangeListener on this EditTextPreference , and whenever user changes it, you will manually save it as an int, so then, getInt will work normally.

android:defaultValue="0"

is a string.

There is no way to declare an actual int in the xml of your prefs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!