我有一些信息存储为SharedPreferences。我需要从一个活动的外部访问该信息(从一个域模型类)。因此,我在Activity中创建了一个静态方法,该方法仅用于获取共享的首选项。
这给了我一些问题,因为显然不可能从静态方法中调用方法“ getSharedPreferences”。
这是蚀给我的信息:
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代码气味吗?
提前致谢。
绝地无双
叮当猫咪
相关分类