0

Log is saying activity cant be paused, and a number format exception, I'm trying to save state of two edit texts that hold numbers,but both can be empty or one can be empty

In my activity there are two edit texts the user can either enter numbers manually and move to the next step or open a calculator to add up some numbers, and set the total to one of the edit texts, i want to save the state so if he needs to fill both edit text using the calculator, the first number he set will still be there when they come back with the second number.

I don't know to deal with this, here is the last bit of code i tried.

protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    double length =Double.parseDouble(edtNumber1.getText().toString());
    double height = Double.parseDouble(edtNumber2.getText().toString());

    outState.putDouble("LENGTH", length);
    outState.putDouble("HEIGHT", height);
}

And in onCreate

if (savedInstanceState != null) {
    double hght = savedInstanceState.getDouble("LENGTH");
    double lnth = savedInstanceState.getDouble("LENGTH");
    if (savedInstanceState.containsKey("LENGTH")); {
        edtNumber1.setText(Double.toString(lnth));
    }

    if(savedInstanceState.containsKey("HEIGHT")); {
        edtNumber2.setText(Double.toString(hght)); 
    }
}
Edward Falk
  • 9,991
  • 11
  • 77
  • 112
Robert
  • 1,107
  • 2
  • 8
  • 8

3 Answers3

1

Try to call super.onSaveInstanceState(outState); at the end of the method and make sure your editText contain numbers.

Thus, change

double hght = savedInstanceState.getDouble("LENGTH");

by

double hght = savedInstanceState.getDouble("HEIGHT");

And delete your ; in if (savedInstanceState.containsKey("LENGTH")); and if (savedInstanceState.containsKey("HEIGHT"));

Alexis C.
  • 91,686
  • 21
  • 171
  • 177
1

IT means that one (or both) of the text fields don't have a number. You need to catch that exception around ParseDouble and deal with it somehow (probably by using a default).

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • Thanks when i give it a default of zero it stops it crashing but it still not saving the state when i navigate away and back, any more suggestions – Robert May 09 '13 at 20:13
1

You have extraneous semi-colons after your "if" statements, turning them into no-ops and causing the blocks that follow to always be executed.

Edward Falk
  • 9,991
  • 11
  • 77
  • 112