0

This error appears when I tring get value from preference:

public static final String PREF_KEY_INTERVAL = "pref_key_interval";
settings = PreferenceManager.getDefaultSharedPreferences(getActivity());
int curInterval =  settings.getInt(PREF_KEY_INTERVAL, 1);

It is also worth noting that app works correctly on emulated device but crashes on real one. I already checked returned type several times. What is the clue or my IDE just goes mad?

preference xml:

<Preference
    android:key="pref_key_interval"
    android:title="@string/pref_key_interval_title"
    android:summary="@string/pref_key_interval_summary"
    android:defaultValue="5"/>
koflox
  • 25
  • 6

2 Answers2

0

android:defaultValue = "5" is a String. So you're casting a String to an integer. Try this

int curInterval =  Integer.parseInt(settings.getString(PREF_KEY_INTERVAL, "1"));
shinilms
  • 1,494
  • 3
  • 22
  • 35
0

Try not to use DefaultSharedPreferences.

final String eulaKey = "mykey";
Context mContext = getApplicationContext();
mPrefs = mContext.getSharedPreferences("myAppPrefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = mPrefs.edit();
editor.putBoolean(eulaKey, true);
editor.commit();

from Android getDefaultSharedPreferences