2016-02-06 31 views
-1

我被我的应用程序的最后一件事困住了。当按钮bSubmit被按下时,除了在EditText框中没有给出输入时强制关闭应用程序而不是强制关闭时,所有的功能都可以工作,我试图放置一个祝酒信息,这也不起作用。我想有一个在我的逻辑缺陷onButtonClick导致部队关闭的缺陷“或逻辑”

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

你能提供的致命日志 –

+0

https://docs.google.com/document/d/1AW2ZDiZmvKgFqYroQWoAKUziwYd- UL4VM0S-iVR4g5g /编辑?usp =分享 –

回答

2

int point = Integer.parseInt(pontstr);将抛出NumberFormatException如果输入不是一个整数(它是一个空字符串""没事的时候是在EditText上,或什么,但一个整数的情况下,值)。这会导致你正在谈论的无声崩溃。

我的建议是将您的parseInt()调用用try-catch块包围起来,并处理调用会引发NumberFormatException的情况。事情是这样的:

int example; try { example = Integer.parseInt(pontstr); } catch (NumberFormatException e) { // Logic to deal with invalid input, maybe toast the user. }

+0

不,这不是问题。即使我根本不使用逻辑OR逻辑或IF逻辑,我的应用程序在没有任何输入时强制关闭。我只需要敬酒,以显示从您的日志中没有给出 –

+0

很明显,您正在获取数字格式异常:导致:java.lang.NumberFormatException:无效int:“” –

+0

我相信您的评论是为其他答案(尽管他也提供了一个有效的观点;你应该检查空弦是否如他所做的那样)。我的答案是在你的if语句之前讨论'int point = Integer.parseInt(pontstr);'。 – jsoberg

2
  1. 您将获得数字格式异常,如果你的字符串不是一个数字。您需要在您的空检查通过后发表声明。
  2. 您正在错误地检查。

    如果(numstr.trim()的isEmpty()|| pontstr.trim()的isEmpty(){ 语境toastcontext = getApplicationContext(); CharSequence的文字= “你还没有把任何价值”; INT持续时间= Toast.LENGTH_SHORT;?

     Toast toast = Toast.makeText(toastcontext,text, duration); 
         toast.show(); 
    
        } 
    

我希望这有助于

+0

感谢您的帮助,但它仍然无法正常工作。我不明白为什么 –