获得“无法解决方法".尝试在Android Studio中实现getSharedPreferences时发生错误 [英] Getting "cannot resolve method" error when trying to implement getSharedPreferences in Android Studio

查看:326
本文介绍了获得“无法解决方法".尝试在Android Studio中实现getSharedPreferences时发生错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个KeyValueDB类,该类存储用于与SharedPreferences进行交互的方法,但是我在定义该类时遇到了一个问题.我只希望构造函数执行的操作是使用正确的文件名存储一个sharedPreferences对象,但是我得到的是无法解析方法'getSharedPreferences(java.lang.String,int)'

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

推荐答案

getSharedPreferences()需要访问上下文.

getSharedPreferences() needs a context to be accessed.

例如:

mContext.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);

您需要将上下文传递到KeyValueDB的构造函数中,或者更好的方法是静态访问它.

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

我会这样做

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

只需对您需要存储的任何信息重复这些get和set方法.

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

其中"this"是对活动的引用.最好在onCreate()方法的每个Activity中设置一个上下文,例如:

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 2016年7月

EDIT July 2016

作为对下面的@RishirajPurohit的响应,设置用户名的操作几乎相同:

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

KeyValueDB.setUsername(mContext, "DesiredUsername");

从那里开始,在上一个静态类中为您完成了所有操作,更改已提交到共享首选项文件,并且可以持久保存并准备通过get方法检索.

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.

请注意一下get方法,以防万一:

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"听起来完全一样.如果在设置用户名之前先调用该get方法,则返回该值.在这种情况下不太有用,但是当您开始使用Integers和Boolean值时,这对于确保鲁棒性非常有用.

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

这篇关于获得“无法解决方法".尝试在Android Studio中实现getSharedPreferences时发生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆