I’m working on a disk space calculator and i need to dynamically calculate some values according to user input. I have an EditText element in my application that receives numbers. I want to call my function calculate() that will calculate other values automatically when user inputs a value in the text field. Here’s my code for it
//fps is my EditText element
this.fps.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
// TODO Auto-generated method stub
calculate();
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
My calculate() function uses the text input, converts it into float and calculates other values like bandwidth. It works fine but the problem comes when I want to delete all numbers in EditText to add another one. The moment there’s nothing in the editText it gives an exception. Which I understand, it’s because there’s nothing to convert into float so parsing exception etc.
I tried putting a default value
if(fps.getText().length==0)
fps.setText("1");
But it doesn’t work. How can I solve this problem?
Just try to surround your
calculate()with try/catch block and catch the exception thrown..