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

元气小坏坏 提交于 2019-12-20 09:39:43

问题


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);
    }

回答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

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.




回答2:


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 ""



来源:https://stackoverflow.com/questions/23351904/getting-cannot-resolve-method-error-when-trying-to-implement-getsharedpreferen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!