0
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","number fail");
        //there is a red mark here^^^^^^^^^(getfloat)
        text.setText(returner);
        //num.set.......how will
        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;
    }

//there is a red mark here^^^^^^^^^(getfloat) -an error that keeps me switching into getInt and getFloat

//num.set.......how will i set a number value?

thanks

Imp Pale
  • 111
  • 1
  • 3
  • 11

3 Answers3

1

you also need to set a default value, when you are trying to getvalues from preferences, like below:

float returnnum = shared.getFloat("sharednum",0.0);
jeet
  • 29,001
  • 6
  • 52
  • 53
0

float returnnum = shared.getFloat("sharednum","number fail");

Change "number fail" to a number of type float. This parameter represents the default value, so you cannot use a string.

Glitch
  • 2,785
  • 1
  • 27
  • 50
idiottiger
  • 5,147
  • 2
  • 25
  • 21
0

try this

  float returnnum = shared.getFloat("sharednum",0f);
AAnkit
  • 27,299
  • 12
  • 60
  • 71