Navinp
Navinp

Reputation: 23

Getting error saving Sharedpreference in Boolean format

Getting an error when saving shared preferences after reading

@Override
public void onCreate( Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mUserLearneddrawer = Boolean.valueOf(readPreferences(getActivity(),KEY_USER_LEARNED_DRAWER,"false"));

}
 public static void readPreferences(Context context,String prefernceName,String defaultValue) {
    SharedPreferences sharedPrefernces = context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPrefernces.edit();
    editor.putString(prefernceName, defaultValue);
    editor.apply();
}

Upvotes: 0

Views: 43

Answers (1)

Vucko
Vucko

Reputation: 7479

You're getting an Exception thrown because you're trying to get a Boolean.valueOf(void) because your readPreferences method returns void, and not Boolean or bool. In order to fix this, you will need to change that method to return what it has read from the preferences. That should look something like this:

public bool readPreferences(Context ctx, String key, boolean default){
     return ctx.getSharedPreferences.getBoolean(key, default);
}

EDIT

As I can see from your edited question, you're actually writing the value in a method called readPreferences which is very counter-intuitive. Anyway, since you're just writing a value and applying just do this:

@Override
public void onCreate( Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    readPreferences(getActivity(),KEY_USER_LEARNED_DRAWER,"false");
}

Do not assign this value to anything, because this does not return anything (returns void).

Upvotes: 2

Related Questions