-1

private int mShuffleMode = SharedPreferences.getInt("shufflemode");

Tells me that a Non-static method can't be referenced from a static context.

I'm not entirely sure what this means.

What I'm trying to do is initialise the variable with the preference that was set before.

3 Answers3

0

getInt() is not a method that you call on the class SharedPreferences. Rather, you call it on an instance of SharedPreferences.

What I'm trying to do is initialise the variable with the preference that was set before.

Retrieve your SharedPreferences object, then call getInt() on that. If you have code that is saving SharedPreferences, you should already have access to that SharedPreferences object, or at least have code for retrieving it.

See the documentation on SharedPreferences for more.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Ah! I get it now. Thank you so much for the answer! Once I have 15 rep, I'll be sure to give your response an upvote. This was very helpful – Brandon Nolet Jan 13 '18 at 23:34
0

You can't get data in a static way using SharedPreferences.getInt("shufflemode");

What you need to do is create an object of SharedPreference and then query it using SharedPreference sharedPreference = context.getSharedPreferences("name-of-preference",MODE) and then `sharedPreference.getInt("shufflemode")

Consider this link

Napster
  • 1,353
  • 1
  • 9
  • 11
0
private SharedPreferences sharedPref ;
private int mShuffleMode;

in your onCreate

sharedPref= context.getSharedPreferences("preferences_file_key", Context.MODE_PRIVATE);
mShuffleMode = sharedPref.getInt("shufflemode");
Amine Choukri
  • 398
  • 2
  • 12