我使用的是SharedPreferences。活动中的编辑器。如何在这个函数中使用getSharedPreferences值?我是java android新手。
public class Dataall {
public static String calc(String nfirst, String nsecond) {
int abn1;
int abn2;
int abn3;
abn1= Integer.parseInt(nfirst);
abn2 = Integer.parseInt(nsecond);
// SharedPreferences show error but its work in another Activity
SharedPreferences prefrs = getSharedPreferences("MY_PRE_NAME",
MODE_PRIVATE);
String langu = prefrs.getString("callss", "");
if(langu.equals("first")){
abn1 = 15;
}else{
abn1 = 20;
}
abn3 = abn1+abn2;
String res = String.valueOf(res);
return res;
}
编辑——我不能在上面的静态函数中使用SharedPreferences。
这里有很多东西我想解开。其中一部分就是解释我认为你想要什么。您有一个类,它正在进行一些计算,需要您获得存储在SharedPreferences中的首选项。问题是,大多数你调用的函数类不能访问Android的上下文(假定你只需要应用程序上下文)。所以你有很多选择,但这里有两个。
1)。快速而肮脏的——将尽可能远的上下文(Activity或Fragment)传递到此方法中,并使用该方法获得共享首选项,如:
public class Dataall {
public static String calc(String nfirst, String nsecond, Context context) {
int abn1;
int abn2;
int abn3;
abn1= Integer.parseInt(nfirst);
abn2 = Integer.parseInt(nsecond);
SharedPreferences prefrs = PreferenceManager.getDefaultSharedPreferences(context);
String langu = prefrs.getString("callss", "");
if(langu.equals("first")){
abn1 = 15;
}else {
abn1 = 20;
}
abn3 = abn1 +a bn2;
String res = String.valueOf(res);
return res;
}
通过DI或Service Location提取共享首选项,或者(最糟糕的选择只是将其列为一种可能性,因为它在技术上有效)使其成为带有包装接口的单例。当然,您需要创建一个自定义应用程序类来获取上下文
//选项1传入使其可模拟
公共类Dataall {public static String calc(String nfirst, String nsecond, issetingsprovider设置){int abn1;int abn2;int abn3;
abn1= Integer.parseInt(nfirst); abn2 = Integer.parseInt(nsecond); String langu = settings.getString("callss"); if("first".equals(langu)){ abn1 = 15; }else { abn1 = 20; } abn3 = abn1 +a bn2; String res = String.valueOf(res); return res; }
}
//选项2假设getFirstInstance在某个有context
的地方被调用public class Dataall {
public static String calc(String nfirst, String nsecond) {
int abn1;
int abn2;
int abn3;
abn1= Integer.parseInt(nfirst);
abn2 = Integer.parseInt(nsecond);
ISettingsProvider settings = SettingsProvider.getInstance();
String langu = settings.getString("callss");
if("first".equals(langu)){
abn1 = 15;
}else{
abn1 = 20;
}
abn3 = abn1 +a bn2;
String res = String.valueOf(res);
return res;
}
}
public final class SettingsProvider implements ISettingsProvider {
public static bool hasBeenInitialized = false
private static ISettingsProvider instance
private SharedPreferences prefs = null
public static ISettingsProvider getFirstInstance(Context: context){
prefs = PreferenceManager.getDefaultSharedPreferences(context);
instance = SettingsProvider();
hasBeenInitialized = true;
}
public static ISettingsProvider getInstance(){
if(instance == null || hasBeenInitialized){
throw new IllegalStateException("Singleton not initialized");
}
return instance;
}
private SettingsProvider(){}
//Overrides
@Override
public String getString(String key, String defaultValue){
return if (defaultValue != null) prefs.getString(key, defaultValue) else prefs.getString(key, "");
}
}
public interface ISettingsProvider{
//put all your get|put methods here
public String getString(String key);
}
改变这一行:来自:
SharedPreferences prefrs = getSharedPreferences("MY_PRE_NAME",
MODE_PRIVATE);
:
SharedPreferences prefrs = getSharedPreferences("MY_PRE_NAME",
Context.MODE_PRIVATE);
答案取自此处:Getting "cannot resolve method"在Android Studio中尝试实现getSharedPreferences时出现错误