我有一个按钮和一个edittext。用户输入一个数字,然后单击该按钮。然后这个数字会发生一些事情。
但是,当编辑文本为空白时,我得到一个错误。我该如何解决?我下面的代码是我的尝试..但它不起作用,因为我收到以下错误并且我的应用程序关闭:
E/AndroidRuntime(324): java.lang.NumberFormatException: 无法将“”解析为整数
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.bBankDeposit:
deposit();
break;
case R.id.bBankWithdraw:
withdraw();
break;
}
}
public void deposit() {
String d = etDepositAmount.getText().toString();
Integer depositAmount = Integer.valueOf(d);
if (depositAmount > playerCash | depositAmount <= 0
| etDepositAmount.getText().toString() == "") {
new AlertDialog.Builder(this).setTitle("Wait")
.setMessage("Please enter a valid deposit value.")
.setPositiveButton("OK", null).show();
etDepositAmount.setText("");
} else {
int newBankBalance = playerBalance + depositAmount;
playerBalance = newBankBalance;
playerCash = (playerCash - depositAmount);
bankBalance.setText("Bank Balance: $" + playerBalance);
cash.setText("Cash in hand: $" + playerCash);
etDepositAmount.setText("");
Player stats = new Player(this);
stats.open();
stats.bankDeposit(playerId, playerCash, playerBalance);
stats.close();
}
}
}