0

I just managed to delete my last question by accident so here it is again.

I have the below class which I chucked together to test the preferences class. The problem I am facing is that when I pass or call from the preferences class nothing happens. The code compiles fine but I'm unable to get or set my preferences. It appears to be getting stuck on SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE); as I have used some print statements to see where it stops. I have tried the using the example on the android site but that does not help. Anyone got any ideas?

public class Preferences extends Activity{

public void getSetPrefs(){

    Preferences p = new Preferences();

    p.setLocationPref("london", "1");

    String location = p.getLocation();
    String location_id = p.getLocation();

}
}

//Preferences class

public class Preferences extends Activity{

    /** Sets location preferences */
public void setLocationPref(String location, int location_id){

    SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString("location", location);
    editor.putInt("location_id", location_id);
    editor.commit();
}

/** Get location from preferences */
public String getLocation(){

    SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
    String location = sharedPreferences.getString("location", "");

    return location;
}

/** Get location ID from preferences */
public int getLocationID(){

    SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
    int locationID = sharedPreferences.getInt("location_id", 0);

    return locationID;
}
}
James
  • 474
  • 4
  • 9
  • 22
  • try posting in meta. You can probably get a mod to roll it back – gobernador Jun 06 '12 at 18:27
  • As I asked in your previous question, is there a reason you're not using getDefaultSharedPreferences(this)? – Tushar Jun 06 '12 at 20:23
  • Yea sorry deleted it, didn't mean to. Just reading up on them. If you get a simple example that you know of I would be grateful :) – James Jun 06 '12 at 21:05

1 Answers1

0

I always retrieve preferences with a call like below:

String location = PreferenceManager.getDefaultSharedPreferences(this).getString("location", "");
Damian
  • 8,062
  • 4
  • 42
  • 43