Я пытаюсь создать один класс, который будет заниматься хранением и извлечением двух радиусов настроек пользователя и кластера.Android SharedPreferences NullPointerException on getSharedPreferences
При загрузке активности «Настройки» я получаю исключение с нулевым указателем.
Отрывок из настроек пользователя:
storage = new Persistence();
radius = (EditText) findViewById(R.id.etRadius);
radius.setText(String.valueOf(storage.getRadius())); <-- Problem
класса, чтобы иметь дело с настойчивостью:
public class Persistence extends Activity {
private static final String PREFERENCES = "tourist_guide_preferences";
private SharedPreferences settings;
private SharedPreferences.Editor editor;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
settings = getSharedPreferences(PREFERENCES, 0);
editor = settings.edit();
}
public int getRadius()
{
return settings.getInt("radius", 2000);
}
public int getClusterSize()
{
return settings.getInt("cluster", 50);
}
public void setRadius(int radius)
{
editor.putInt("radius", radius);
editor.commit();
}
public void setClusterSize(int size)
{
editor.putInt("cluster", size);
editor.commit();
}
}
Пожалуйста, проверьте здесь: этот лучший лучший пример http://stackoverflow.com/questions/5734721/android-shared-preferences –