Skip to content Skip to sidebar Skip to footer

Getting "cannot Resolve Method" Error When Trying To Implement Getsharedpreferences In Android Studio

I'm trying to create a class KeyValueDB which stores methods for interacting with SharedPreferences, however I'm running into a problem just defining the class. All I want the con

Solution 1:

getSharedPreferences() needs a context to be accessed.

For instance:

mContext.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);

You need to either pass the context into the constructor for KeyValueDB, or a better way would be to access that statically.

I would do this

publicclassKeyValueDB {
privateSharedPreferences sharedPreferences;
privatestaticStringPREF_NAME = "prefs";

    publicKeyValueDB() {
    // Blank
    }

    privatestaticSharedPreferencesgetPrefs(Context context) {
        return context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
    }

    publicstaticStringgetUsername(Context context) {
        returngetPrefs(context).getString("username_key", "default_username");
    }

    publicstaticvoidsetUsername(Context context, String input) {
        SharedPreferences.Editor editor = getPrefs(context).edit();
    editor.putString("username_key", input);
    editor.commit();
    }
}

Just repeat those get and set methods for any information you need to store.

To access them from an Activity, you would do this:

Stringusername= KeyValueDB.getUsername(this);

Where "this" is a reference to the Activity. It's also good practice to setup a context in each Activity in the onCreate() method, like:

publicclassmyActivityextendsActivity{

Context mContext;

publicvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mContext = this;

    Stringusername= KeyValueDB.getUsername(mContext);
}

EDIT July 2016

in response to @RishirajPurohit below, to set a username you do very much the same thing:

KeyValueDB.setUsername(mContext, "DesiredUsername");

From there everything is done for you in the previous static class, the change is committed to the shared preferences file and persisted and ready to be retrieved by the get method.

Just a note on the get method, in case anyone wonders:

publicstaticStringgetUsername(Context context) {
    returngetPrefs(context).getString("username_key", "default_username");
}

"default_username" is exactly as it sounds. If that get method is called first before a username is set, that is the value that is returned. Less useful in this instance, but when you start using Integers and Boolean values this is very useful for ensuring robustness.

Solution 2:

You should pass a context there...

example of getting a string value:

publicstaticStringgetUserName(Context ctx)
{
    returngetSharedPreferences(ctx).getString(PREF_USER_NAME, "");
}

where PREF_USER_NAME is the key and the second parameter is when it cant find the key it returns ""

Post a Comment for "Getting "cannot Resolve Method" Error When Trying To Implement Getsharedpreferences In Android Studio"