5
    public void onClick(View v) {
    switch(v.getId()){
    case R.id.save2:
        s = text.getText().toString();
        number = Float.parseFloat(num.getText().toString());
        SharedPreferences.Editor editor = shared.edit();
        editor.putString("sharedtext",s);
        editor.putFloat("sharednum",number);
        editor.commit();
        Toast.makeText(this,"save2",Toast.LENGTH_SHORT).show();
        break;
    case R.id.load2:
        String returner = shared.getString("sharedtext","Returner fail");
        float returnnum = shared.getFloat("sharednum",0);
        text.setText(returner);
        num.setText(returnnum); //error here
        Toast.makeText(this,"load2",Toast.LENGTH_SHORT).show();
        break;
    case R.id.page2:
        intent= new Intent(this,SharedPreferencesActivity.class);
        startActivity(intent);
        Toast.makeText(this,"page2",Toast.LENGTH_SHORT).show();
        break;
    }

how could i resolve this error?

how to make something like setInt(int num);

btw the variable num and text are both EditText

Squonk
  • 48,735
  • 19
  • 103
  • 135
Imp Pale
  • 111
  • 1
  • 3
  • 11
  • http://sspower3.blogspot.com/2011/11/sharedpreferences-in-eazy-way.html check this may be it will be helpful for you – Andy Jan 18 '12 at 09:32
  • this is interesting though, because before any knowledge in sharedpreferences i'm thinking of managing the R.string.... BUT everyone says it is static and its values can't change, i'm confused in the blogspot – Imp Pale Jan 18 '12 at 10:14
  • I think you are going to confuse by {user name} this statement. check very carefully then you will find the solution. – Andy Jan 18 '12 at 10:24
  • but that is just what located in the R.strings.xml? right? – Imp Pale Jan 18 '12 at 10:38
  • Yes... and i think u must be aware about the working of sharedpref*. now make the link between them. – Andy Jan 18 '12 at 10:41
  • "now make the link between them." what does this means? – Imp Pale Jan 18 '12 at 11:00
  • well anyway i'm using sharedpreferences in float like height and width to be used for some application function – Imp Pale Jan 18 '12 at 11:01

1 Answers1

11

If you pass a numeric value as the text to a text field, Android will try to interpret it as a resource Id. Make it a text first. The preferred way is to do:

num.setText(String.valueOf(returnnum));

(For good practices on conversion to string, check this post)

Community
  • 1
  • 1
Guillaume
  • 22,694
  • 14
  • 56
  • 70