我正在构建一个包含 SharedPreferences
的 android 项目。
我的 SharedPreferences
工作正常,我在多个 Activity 中对其进行了测试。但是在我为全局变量定义的类中,定义 SharedPreferences
将导致强制关闭(eclipse 没有在代码中显示任何错误)。
public class Globals extends Application {
final SharedPreferences s = getSharedPreferences("Prefs", MODE_PRIVATE);
}
问题是什么?
最佳答案
您应该传递 Context
并使用
SharedPreferences prefs = Context.getSharedPreferences(
"首选项", Context.MODE_PRIVATE);
关于android - SharedPreferences在android程序中产生强制关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20444459/