Search code examples
javaandroidopen-source

Android - Initialize variable with currently set preference


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.


Solution

  • 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.