I have a preferences.xml
which contains the following definition:
<ListPreference
android:title="@string/LimitSetting"
android:summary="@string/LimitSettingText"
android:key="limitSetting"
android:defaultValue="10"
android:entries="@array/limitArray"
android:entryValues="@array/limitValues" />
and with values defined as follows:
<string-array name="limitArray">
<item>1 %</item>
<item>3 %</item>
<item>5 %</item>
<item>10 %</item>
<item>20 %</item>
</string-array>
<string-array name="limitValues">
<item>1</item>
<item>3</item>
<item>5</item>
<item>10</item>
<item>20</item>
</string-array>
which is being called in an activity as follows:
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
int offsetProgressInitial = sharedPref.getInt("limitSetting", 10);
So far so good, but when the code gets actually called I get this error:
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at android.app.SharedPreferencesImpl.getInt(SharedPreferencesImpl.java:239)
at com.test.app.NewEntryActivity.onCreate(NewEntryActivity.java:144)
at android.app.Activity.performCreate(Activity.java:5977)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2258)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365)
at android.app.ActivityThread.access$800(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
This error does not make any sense to me. The list only contain values that can be converted into an int, and the default values given in the xml file and in the code also represents just a number. So why do I get this error, and how to fix it?
integer-array
instead?android:defaultValue="10"
?