-1

I have an age input edit text which i want to put user's previous input there using shared preferences (for after first input). i have written following code but it force closes. what is the problem??

  @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

//setting text for editText
age_editText.setText(my_prefs.getInt("age_value", 0) + ""); //when i delete this part, it doesn't force close

btn_calculate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                try {
                    Int age_value = Integer.parseInt(age_editText.getText().toString());
                    //shared preferences
                    sharedPreferences my_prefs = getActivity().getPreferences(Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = my_prefs.edit();
                    editor.putInt("age_value", age_value);
                    editor_bmi.commit();

                } catch (Exception e) {
                    Toast.makeText(getContext(), getString(R.string.please_fill_all_inputs), Toast.LENGTH_LONG).show();
                }
        }
    });

3 Answers3

1

You need to use getSharedPreferences() in place of getPreferences() as shown below.

SharedPreferences sharedPreferences = getSharedPreferences("name", MODE_PRIVATE);

Note: getPreferences() and getSharedPreferences() are two different methods that return different Preference objects.

Varun Kumar
  • 1,241
  • 1
  • 9
  • 18
0

Change

sharedPreferences my_prefs = getActivity().getPreferences(Context.MODE_PRIVATE);

to

sharedPreferences my_prefs = getActivity().getSharedPreferences("my_pref", Context.MODE_PRIVATE);

and write this above

age_editText.setText(my_prefs.getInt("age_value", 0) + "");

proper Way to do That.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    sharedPreferences my_prefs getActivity().getSharedPreferences("my_pref", Context.MODE_PRIVATE);
    age_editText.setText(my_prefs.getInt("age_value", 0) + ""); //when i delete this part, it doesn't force close

    btn_calculate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {

                Int age_value = Integer.parseInt(age_editText.getText().toString());

                SharedPreferences.Editor editor = my_prefs.edit();
                editor.putInt("age_value", age_value);
                editor_bmi.commit();

            } catch (Exception e) {
                Toast.makeText(getContext(), getString(R.string.please_fill_all_inputs), Toast.LENGTH_LONG).show();
            }
        }
    });
}
V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50
  • 1
    `sharedPreferences my_prefs = getActivity().getSharedPreferences("my_pref", Context.MODE_PRIVATE);` You missed `=` to create an object. – Pramod Waghmare Jul 28 '16 at 07:40
0

You can save your age into SharedPreferences by using below method :

public void saveAge(int age_value) {
        SharedPreferences pre = getSharedPreferences("my_age", MODE_PRIVATE);
        SharedPreferences.Editor editor = pre.edit();
        editor.putInt("age_value", age_value);
        editor.commit();
    }

And when you want to get your age, use this :

public int readAge() {
        SharedPreferences pre = getSharedPreferences("my_age", MODE_PRIVATE);
        int age = pre.getInt("age_value", 0);
        return age;
    }
Dang Nguyen
  • 354
  • 2
  • 9