то, что сказал Шайфул, верно. Попробуйте использовать YourActivityName.this.getSharedPreferences(myPrefs,Context.MODE_PRIVATE); Shabbir Panjesha 31.05.2012
Если вы создаете объект своего класса LinearLayout в классе Activity, вы должны передать контекст в конструкторе.
Ниже приведен снайпер
Класс активности
public class SampleActivity extends Activity
{
public void onCreate(Bundle bundle)
{
MyLinearLayout layout = new MyLinearLayout(this);
-----
-----
}
}
Класс MyLinearLayout
public class MyLinearLayout extends LinearLayout {
private Context context;
public MyLinearLayout(Context context) {
super(context);
this.context=context;
SharedPreferences preferences=context.getSharedPreference("pref",
context.MODE_PRIVATE);
}
}
Ответ 2 · рейтинг 1 · Shaiful
Удалите ключевое слово this (замените this.getSharedPreferences() на getSharedPreferences(). this относится к внутреннему классу View.onClickListener(), тогда как метод фактически находится в классе Activity.
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) {
clearSharedPreferences(context);
showProgressBar();
}
}
};
Ответ 6 · DarckBlezzer
Примеры удаления SharedPreference, одной переменной или всех SharedPreference...
SharedPreferences sharedPreferences = getSharedPreferences("mysharedpreferences",MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
// a specific var of sharedPreference....
//Mark in the editor that a preference value should be removed, which will be done in the actual preferences once commit() is called.
editor.remove("mySharedVar");
//if you want remove all sharedPreference...
//Mark in the editor to remove all values from the preferences. Once commit is called, the only remaining preferences will be any that you have defined in this editor.
editor.clear();
editor.apply();
Ответ 7 · Md. Rejaul Karim
Вы можете попробовать эту связку кодов, пожалуйста. Эти строки работали на меня. Надеюсь, это сработает и для вас. ^_^