[英]Flawed “or logic” in onButtonClick causing force close
我為我的應用程序所困擾。 當按下按鈕bSubmit時,所有功能均起作用,除了在EditText框中未提供任何輸入的情況下強制關閉應用程序,而不是強制關閉時,我嘗試放置烤面包味精,該方法也不起作用。 我想我的邏輯有缺陷
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();
}
int point = Integer.parseInt(pontstr);
如果輸入不是Integer(當EditText中沒有內容或Integer值時,則為空字符串""
時)將引發NumberFormatException
。 這將導致您正在談論的無提示崩潰。
我的建議是用一個try-catch塊包圍您的parseInt()
調用,並處理該調用將引發NumberFormatException的情況。 像這樣:
int example; try { example = Integer.parseInt(pontstr); } catch (NumberFormatException e) { // Logic to deal with invalid input, maybe toast the user. }
您檢查的方式有誤。
如果(numstr.trim()。isEmpty()|| pontstr.trim()。isEmpty(){上下文toastcontext = getApplicationContext(); CharSequence text =“您沒有輸入任何值”; int持續時間= Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(toastcontext,text, duration); toast.show(); }
我希望這有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.