Android : Getting error “cannot resolve getSharedPreferences(java.lang.String, int)”

2018-01-24 00:02:21

I'm trying to access the SharedPreferences in a non-activity class but I always get this error cannot resolve getSharedPreferences(java.lang.String, int) . But when I use the getSharedPreferences in normal activity class I don't get this error.

错误代码:

 SharedPreferences prefs = getSharedPreferences("MyPrefs", context.MODE_PRIVATE);

解决办法:

 SharedPreferences prefs = context.getSharedPreferences("MyPrefs", context.MODE_PRIVATE);



或许在activity中不需要context,但是在fragement中需要getContext

  • 2018-01-29 15:18:57

    Druid多数据源配置

    搞了一天,竟然输在了把datasource,携程dataSource.老是切换不成功,几近崩溃。