【发布时间】:2012-06-05 14:00:08
【问题描述】:
我想清除存储在共享首选项中的值 我正在使用此代码。
/* SharedPreferences myPrefs = this.getSharedPreferences("myPrefs",
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = myPrefs.edit();
editor.clear();
editor.commit(); */
但收到此错误。
The method getSharedPreferences(String, int) is undefined for the type new View.OnClickListener(){}
私有 OnClickListener logoBarListener = new OnClickListener() { /* *(非 Javadoc) * * @see android.view.View.OnClickListener#onClick(android.view.View) */ public void onClick(View v) {
if (v.getId() == R.id.img_bottom_home) {
showProgressBar(MainScreen.class);
} else if (v.getId() == R.id.img_bottom_basket) {
showProgressBar(ShopBasketGet.class);
} else if (v.getId() == R.id.img_bottom_notification) {
showProgressBar(Notification.class);
} else if (v.getId() == R.id.img_bottom_login) {
SharedPreferences myPrefs = getSharedPreferences("myPrefs",
MODE_WORLD_READABLE);
SharedPreferences.Editor editor = myPrefs.edit();
editor.clear();
editor.commit();
showProgressBar();
}
}
};
【问题讨论】:
-
Shaiful 说的是对的。尝试使用 YourActivityName.this.getSharedPreferences("myPrefs",Context.MODE_PRIVATE);