I have two fields that i want to calculate. The program works fine if i put in numbers in both fields, but if i have one empty and presses my "buttonSubtract" (calc-button) it just shuts down with "application closed...". Why doesn´t the catch-clause catch the exception?
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonSubtract = (Button) findViewById(R.id.buttonsubtract);
editTextNumber1 = (EditText) findViewById(R.id.textnumber1);
editTextNumber2 = (EditText) findViewById(R.id.textnumber2);
textSum = (TextView) findViewById(R.id.textViewSum);
try
buttonSubtract.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
if (editTextNumber1.getText().toString() == ""
|| editTextNumber2.getText().toString() == "")
{
textSum.setText("Put numbers in both fields");
} else
{
int sum = Integer.parseInt(editTextNumber1.getText()
.toString())
- Integer.parseInt(editTextNumber2.getText()
.toString());
textSum.setText("sum: " + sum);
}
}
});
} catch (NumberFormatException e)
{
textSum.setText("Put numbers in both fields");
}