Java EditText为空时出错(无法将';';解析为整数)
我有一个按钮和一个编辑文本。用户输入一个数字,然后单击按钮。然后这个数字发生了一些变化 但是,当edittext为空时,我得到一个错误。我该如何解决这个问题?下面的代码是我的尝试..但它不起作用,因为我出现以下错误并且我的应用程序关闭: E/AndroidRuntime(324):java.lang.NumberFormatException:无法将“”解析为整数Java EditText为空时出错(无法将';';解析为整数),java,android,numberformatexception,Java,Android,Numberformatexception,我有一个按钮和一个编辑文本。用户输入一个数字,然后单击按钮。然后这个数字发生了一些变化 但是,当edittext为空时,我得到一个错误。我该如何解决这个问题?下面的代码是我的尝试..但它不起作用,因为我出现以下错误并且我的应用程序关闭: E/AndroidRuntime(324):java.lang.NumberFormatException:无法将“”解析为整数 public void onClick(View arg0) { // TODO Auto-generated method
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();
}
}
public void onClick(视图arg0){
//TODO自动生成的方法存根
开关(arg0.getId()){
案例R.id.BB银行存款:
存款();
打破
案例R.id.BBA撤销:
撤回();
打破
}
}
公众作废保证金(){
字符串d=etDepositAmount.getText().toString();
整数存款金额=整数。值(d);
如果(depositAmount>playerCash | depositAmount解决方案背后的想法是正确的,那么只会出现一些小的语法错误
执行Integer.valueOf(string)
时,需要处理数字格式异常。这只是Java让您知道它可以可靠地为给定字符串返回整数的方法
|
是一种按位OR运算。您可能需要使用布尔逻辑进行比较的|
。其次,在比较字符串时,您希望通过值而不是引用进行比较。==
通过引用进行比较,而someString.equals(someOtherString)
将实际检查字符串的内容。但您确实不需要这样做,因为您已经将其解析为整数
通过这些修复,您将了解输入是否不正确,并在必要时显示警报
所以,完整的解决方案是
public void deposit() {
String d = etDepositAmount.getText().toString();
Integer depositAmount = 0;
try
{
depositAmount = Integer.valueOf(d);
}
catch(NumberFormatException ex)
{
// Uh oh! Bad input!
}
if (depositAmount > playerCash || depositAmount <= 0)
{
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();
}
}
公共作废押金(){
字符串d=etDepositAmount.getText().toString();
整数存款金额=0;
尝试
{
存款金额=整数。值为(d);
}
捕获(NumberFormatException ex)
{
//哦,输入错误!
}
如果(存款金额>玩家现金| |存款金额您必须捕获NumberFormatException
String s ="";
int i=0;
try{
i=Integer.parseInt(s);
}catch(NumberFormatException ex){
//Some toast or alert
}
您必须检查编辑文本的空值以解决NumberFormatException
String s ="";
int i=0;
try{
i=Integer.parseInt(s);
}catch(NumberFormatException ex){
//Some toast or alert
}
有关检查null
或blank
if(!TextUtils.isEmpty(text)) {
//Use it
}else {
//display validation message
}
或者,从API第9级开始,您可以使用。如果d
为空,您可能需要包含else
语句。