0

All I simply want to do is save whatever calorie number the user sets in the EditText. I've tried for way too long to implement savedpreferences.

This calorie figure will be passed to another activity, I just want to have the value remain there so if you switch activities or exit/enter the app the number stays.

You'll see my guesses of what the code should be with a couple of apostrophes in front of them.Currently, without the apostrophes it builds but errors before even starting.

When solving, could you add it straight into the code? I may have totally messed up the placement of all of this. (a save button would also work) Thanks!

public class Settings extends ActionBarActivity {

//public static final String MY_PREFS_NAME = "MyPrefsFile";

EditText calorieSet;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);



    //SharedPreferences sharedPreferences = getApplication().getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
    //String calValue = sharedPreferences.getString("calValue", "default");
    //calorieSet.setText( calValue );


}


public void onClick(View view) {

    Intent i = new Intent(this, MainActivity.class);

    final EditText calorieSet = (EditText) findViewById(R.id.calorieSet);
    String userCals= calorieSet.getText().toString();
    i.putExtra("calorieSet",userCals );


    if (userCals == null || userCals.equals("")) {
        Toast.makeText(this, "Please enter a calorie number", Toast.LENGTH_SHORT).show();
        return;
    }


    //SharedPreferences sharedPreferences = getApplication().getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
    //SharedPreferences.Editor editor = sharedPreferences.edit();
    //editor.putString("calValue", calorieSet.getText().toString());
    //editor.commit();

    startActivity(i);
}

1 Answer 1

2

You need to initialize the calorieSet EditText variable in your onCreate() function before setting the text value:

public class Settings extends ActionBarActivity {

public static final String MY_PREFS_NAME = "MyPrefsFile";

EditText calorieSet;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);

    // add this line: 
    calorieSet = (EditText) findViewById(R.id.calorieSet);

    SharedPreferences sharedPreferences = getApplication().getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
    String calValue = sharedPreferences.getString("calValue", "default");
    calorieSet.setText( calValue );


}


public void onClick(View view) {

    Intent i = new Intent(this, MainActivity.class);

    // This line not necessary as you have already set the variable in onCreate():
    //final EditText calorieSet = (EditText) findViewById(R.id.calorieSet);

    String userCals= calorieSet.getText().toString();
    i.putExtra("calorieSet",userCals );


    if (userCals == null || userCals.equals("")) {
        Toast.makeText(this, "Please enter a calorie number", Toast.LENGTH_SHORT).show();
        return;
    }


    SharedPreferences sharedPreferences = getApplication().getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString("calValue", calorieSet.getText().toString());
    editor.commit();

    startActivity(i);
}

Check the LogCat if you get errors like this, it will give you hints about what is going wrong (e.g. NullPointerException) and what line of code the error is occurring on. Or step through with a debugger.

2
  • Easy fix, but such a hassle I didn't see that sooner! Thanks! Quick follow up, can I have it say something other than "default" for the first time it's opened? Maybe leave it empty or put in another value at first like 2000 when initialized?
    – pjhollow
    Apr 21, 2015 at 1:41
  • You can also use "" as the default.
    – samgak
    Apr 21, 2015 at 2:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.