对于SharedPreferences.Editor类型,方法apply()未定义

时间:2013-01-15 04:41:20

标签: java android

代码:http://pastebin.com/sh5TKhVv 我已经从书中复制了这段代码而且我没有改变任何东西,但它在第96和192行中有这个错误。

preferencesEditor.apply(); --> The method apply() is undefined for the type SharedPreferences.Editor

3 个答案:

答案 0 :(得分:1)

API LEVEL 9 中添加了

SharedPreferences.Editor.apply();。如果您使用的是9的较低版本,则使用SharedPreferences.Editor. commit()保存所有包含SharedPreferences的内容

答案 1 :(得分:0)

shared_preferences= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
editor =shared_preferences.edit();
editor.putString("test","Done");
editor.commit();

答案 2 :(得分:0)

虽然我正在使用API​​ LEVEL 17,但我遇到了同样的问题。
这条线解决了我的问题:

SharedPreferences.Editor.commit();