Edit Text shows exception Invalid Int “”

前端 未结 5 1160
南方客
南方客 2020-12-07 04:50

I have already tried setting the EditText default value to be zero but when it comes to editing and erase as \"\" ( empty space , no more wordings _ in the Edit Text field <

相关标签:
5条回答
  • 2020-12-07 05:17

    Surround the offending lines with a try/catch block.

    try {
        Log.d("Month" , monthlyTest);
        Log.d("freeMonths" , freeMonths);                               
        monthlyFeeforChannel =  Integer.parseInt(monthlyTest);                                      
        MonthlyFee.add(monthlyFeeforChannel);
        FreePeriod.add(freeMonths);
    }
    catch (NumberFormatException nfe) {
        nfe.printStackTrace();
        // Alert the user/log the error, etc.
    }
    
    0 讨论(0)
  • 2020-12-07 05:19

    If the input type is set to number you cannot test for "null" so the best solution is to test if length is zero, this way:

    if(number.length() == 0) {EditText is empty!!!}
    
    0 讨论(0)
  • 2020-12-07 05:31

    I think, you should have to add this to your edit text

    editText.setHint("0");
    
    0 讨论(0)
  • 2020-12-07 05:36

    In the aboue code you have used like converting String to Integer in that case you will not convert ""(Empty String) to Integer

    Integer.parseInt(monthlyTest);  // Here monthyTest value is ""
    

    Please make sure the string is not empty befor conversion.

    Check the length of the String is should be greater than 0.

    if(monthlyTest.length()>0)
    

    Thanks.....

    0 讨论(0)
  • 2020-12-07 05:37

    try

    if(monthlyTest == null || monthlyTest.trim().equals("")){
    

    You could also use

    if(monthlyTest == null || monthlyTest.trim().isEmpty()){
    
    0 讨论(0)
提交回复
热议问题