我有一些信息存储为 SharedPreferences。我需要从 Activity 外部访问该信息(从域模型类中)。因此,我在 Activity 中创建了一个静态方法,仅用于获取共享首选项。
这给了我一些问题,因为显然不可能从静态方法调用方法“getSharedPreferences”。
这是eclipse给我的信息:
Cannot make a static reference to the non-static method
getSharedPreferences(String, int) from the type ContextWrapper
我试图通过使用 Activity 实例来解决这个问题,如下所示:
public static SharedPreferences getSharedPreferences () {
Activity act = new Activity();
return act.getSharedPreferences("FILE", 0);
}
此代码给出了一个空点异常。
有解决办法吗?我是否会通过尝试执行此操作进入 android-code-smell ?
提前致谢。