Getting "cannot resolve method" error when trying to implement getSharedPreferences in Android Studio

StackOverflow https://stackoverflow.com/questions/23351904

  •  11-07-2023
  •  | 
  •  

Question

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 constructor to do is store a sharedPreferences object with the correct filename, but I'm getting a "cannot resolve method 'getSharedPreferences(java.lang.String,int)'

I am passing a String and an int... I'm not sure what I'm doing wrong. Appreciate any help!

package com.farmsoft.lunchguru.utils;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.json.JSONException;

/**
 * Created by sxs on 4/28/2014.
 */
public class KeyValueDB {
    private SharedPreferences sharedPreferences;

    public KeyValueDB(String prefName) {
        sharedPreferences = getSharedPreferences(prefName, Context.MODE_PRIVATE);
    }
Was it helpful?

Solution

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

public class KeyValueDB {
private SharedPreferences sharedPreferences;
private static String PREF_NAME = "prefs";

    public KeyValueDB() {
    // Blank
    }

    private static SharedPreferences getPrefs(Context context) {
        return context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
    }

    public static String getUsername(Context context) {
        return getPrefs(context).getString("username_key", "default_username");
    }

    public static void setUsername(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:

String username = 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:

public class myActivity extends Activity{

Context mContext;

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

    String username = 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:

public static String getUsername(Context context) {
    return getPrefs(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.

OTHER TIPS

You should pass a context there...

example of getting a string value:

public static String getUserName(Context ctx)
{
    return getSharedPreferences(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 ""

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top