1

I'm trying to create a single class which will deal with storing and retrieving two user settings 'radius' and 'cluster'.

Upon loading the 'Settings' activity I get a null pointer exception.

Snippet from the user settings:

    storage = new Persistence();
    radius = (EditText) findViewById(R.id.etRadius);        
    radius.setText(String.valueOf(storage.getRadius()));  <-- Problem

Class to deal with persistence:

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();        
    }   
}
Jack
  • 15,614
  • 19
  • 67
  • 92
  • 1
    Please checkout here : this best best example http://stackoverflow.com/questions/5734721/android-shared-preferences – Mayur Bhola Mar 08 '12 at 18:41

2 Answers2

1

Your Persistence class should not be an Activity. You should make it a normal class and put the code of its onCreate inside this normal class constructor.

Change it to look like this:

public class Persistence { 

    private static final String PREFERENCES = "tourist_guide_preferences";
    private SharedPreferences settings;
    private SharedPreferences.Editor editor;
    private Context context;


    public Persistence(Context context)
    {
        this.context = context;
        settings = context.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();        
    }   
}

And in your Activity, you instantiate this Persistence class like this:

storage = new Persistence(this);
Tiago Pasqualini
  • 821
  • 5
  • 12
  • I tried that first, i couldn't use the 'getSharedPreferences' function. Is there something else that I should be using instead? – Jack Mar 08 '12 at 18:40
  • You probably didn't had the context to call getSharedPreferences. You can call this method from the context variable. – Tiago Pasqualini Mar 08 '12 at 18:40
1

storage = new Persistence(); this won't call the onCreate of the Persistence activity. It would be better you create a general class. You create a context variable and use that to create the sharedpreference instance. You should be calling this general class from an activity class

arjoan
  • 1,849
  • 2
  • 20
  • 39