我坚持使用我的应用程序的最后一件事。当按下按钮bSubmit时,所有的功能都工作,除了在EditText框中没有给出输入时强制关闭,而不是强制关闭,我试图放一个toast msg,这也不起作用。我想我的逻辑存在缺陷
if (v.getId() == R.id.bSubmit) {
String numstr = numberEditText.getText().toString();
String pontstr = pointEditText.getText().toString();
int point = Integer.parseInt(pontstr);
//takes in the customer's number and the purchase amount
//search for the customer's number in database
DatabaseHelper helper = new DatabaseHelper(this);
Log.d("onButtonClick",""+numstr);
customer = helper.searchCustomer(numstr);
if (numstr.equals(null) || pontstr.equals(null)){
Context toastcontext = getApplicationContext();
CharSequence text ="You haven't put any value";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(toastcontext,text, duration);
toast.show();
}
if (customer.getuNum() != null && customer.getuNum().equals(numstr)) {
//if found then add the amount as new points
//int row = helper.searchRow(customer.getuNum());
// int point = helper.searchPoint(customer.getuNum());
int pnt = customer.getPoint() + point;
Log.d("MainActivity",customer.getuNum()+" pnt "+pnt);
helper.updatePoint(pnt,customer.getuNum());
currentPoint.setText("Your point is "+pnt);
}
else {
//create a new customer and add the info to database
Customer customer = new Customer();
customer.setuNum(numstr);
customer.setPoint(point);
helper.insertCustomer(customer);
Log.d("MainActivity","customer null num "+customer.getuNum()+" point "+point);
currentPoint.setText("Your point is " + point);
}
numberEditText.getText().clear();
pointEditText.getText().clear();
}
答案 0 :(得分:2)
int point = Integer.parseInt(pontstr);
将抛出NumberFormatException
(如果在EditText中没有任何内容,则为空字符串""
,或者除了整数之外的任何内容)值)。这会导致您正在谈论的无声崩溃。
我的建议是用try-catch块围绕你的parseInt()
调用,并处理调用会抛出NumberFormatException的情况。像这样:
int example;
try {
example = Integer.parseInt(pontstr);
} catch (NumberFormatException e) {
// Logic to deal with invalid input, maybe toast the user.
}
答案 1 :(得分:2)
您正在以错误的方式进行检查。
if(numstr.trim()。isEmpty()|| pontstr.trim()。isEmpty(){ Context toastcontext = getApplicationContext(); CharSequence text ="你没有放任何价值&#34 ;; int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(toastcontext,text, duration);
toast.show();
}
我希望这会有所帮助。