0

Good day,

I have a problem - I have a PreferenceScreen with a ListPreference that has default values. Then, I have an Activity that gets a value from that ListPreference, but it returns "null" if I do not open PreferenceScreen first.

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
    String prefSize = sp.getString("size", null);
    Size = Integer.parseInt(prefSize);
    mySize = Size;

Is it possible to do something about it? If I open my PreferenceScreen and then go to the activity it returns the default value, but if I open my Activity during the first run before I open the PreferenceScreen (just open, not changing any setttings) it the app crashes.

Thank you so much!

Codescape
  • 15
  • 4

1 Answers1

0

try this

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
String prefSize = sp.getString("size", "1");//provide deafult values to parse into int for first time
Size = Integer.parseInt(prefSize);
mySize = Size;
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • Thank you! Solved the problem, but I wonder if there is a way to initialise the settings from the PreferenceScreen! – Codescape Jul 18 '17 at 16:00