Edit Text shows exception Invalid Int “”

2019-01-12 11:55发布

问题:

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

It shows exception Invalid Int "" when pressing submitt button . Could you please tell me how to resolve this exception ?

The below is my code

    for (int j = 0; j < selectedChannels; j++) {

        lconper = (Spinner) findViewById (j+120);
        int monthlyFeeforChannel = 0;       
        contractPeriodSelected.add((String) lconper.getSelectedItem());

        String monthlyTest = txtMonthlyFee[j].getText().toString().trim();
        String freeMonths = txtFreePeriod[j].getText().toString().trim();

        if(monthlyTest.trim()==null || monthlyTest.trim()==""){
            new AlertDialog.Builder(this)
            .setTitle("Warning")
            .setMessage(
                    "You must fill in Monthly Fees for subscribed channels!")
                    .setNeutralButton("OK",
                            new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int id) {
                            // if this button is clicked, close
                            // current activity
                            dialog.cancel();
                        }});
            break;
        }
        else
        {
            Log.d("Month" , monthlyTest);
            Log.d("freeMonths" , freeMonths);                               
            monthlyFeeforChannel =  Integer.parseInt(monthlyTest);                                      
            monthlyFee.add(monthlyFeeforChannel);
            FreePeriod.add(freeMonths);     
        }   
    }

Logcat

    06-20 13:52:03.711: E/AndroidRuntime(17713): FATAL EXCEPTION: main
06-20 13:52:03.711: E/AndroidRuntime(17713): java.lang.IllegalStateException: Could not execute method of the activity
06-20 13:52:03.711: E/AndroidRuntime(17713):    at android.view.View$1.onClick(View.java:3130)
06-20 13:52:03.711: E/AndroidRuntime(17713):    at android.view.View.performClick(View.java:3652)
06-20 13:52:03.711: E/AndroidRuntime(17713):    at android.view.View$PerformClick.run(View.java:14354)
06-20 13:52:03.711: E/AndroidRuntime(17713):    at android.os.Handler.handleCallback(Handler.java:605)
06-20 13:52:03.711: E/AndroidRuntime(17713):    at android.os.Handler.dispatchMessage(Handler.java:92)
06-20 13:52:03.711: E/AndroidRuntime(17713):    at android.os.Looper.loop(Looper.java:137)
06-20 13:52:03.711: E/AndroidRuntime(17713):    at android.app.ActivityThread.main(ActivityThread.java:4519)
06-20 13:52:03.711: E/AndroidRuntime(17713):    at java.lang.reflect.Method.invokeNative(Native Method)
06-20 13:52:03.711: E/AndroidRuntime(17713):    at java.lang.reflect.Method.invoke(Method.java:511)
06-20 13:52:03.711: E/AndroidRuntime(17713):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
06-20 13:52:03.711: E/AndroidRuntime(17713):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:561)
06-20 13:52:03.711: E/AndroidRuntime(17713):    at dalvik.system.NativeStart.main(Native Method)
06-20 13:52:03.711: E/AndroidRuntime(17713): Caused by: java.lang.reflect.InvocationTargetException
06-20 13:52:03.711: E/AndroidRuntime(17713):    at java.lang.reflect.Method.invokeNative(Native Method)
06-20 13:52:03.711: E/AndroidRuntime(17713):    at java.lang.reflect.Method.invoke(Method.java:511)
06-20 13:52:03.711: E/AndroidRuntime(17713):    at android.view.View$1.onClick(View.java:3125)
06-20 13:52:03.711: E/AndroidRuntime(17713):    ... 11 more
06-20 13:52:03.711: E/AndroidRuntime(17713): Caused by: java.lang.NumberFormatException: Invalid int: ""
06-20 13:52:03.711: E/AndroidRuntime(17713):    at java.lang.Integer.invalidInt(Integer.java:138)
06-20 13:52:03.711: E/AndroidRuntime(17713):    at java.lang.Integer.parseInt(Integer.java:359)
06-20 13:52:03.711: E/AndroidRuntime(17713):    at java.lang.Integer.parseInt(Integer.java:332)
06-20 13:52:03.711: E/AndroidRuntime(17713):    at com.test.Eystem1.Confirm.submitOrder(Confirm.java:415)
06-20 13:52:03.711: E/AndroidRuntime(17713):    ... 14 more

回答1:

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.....



回答2:

try

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

You could also use

if(monthlyTest == null || monthlyTest.trim().isEmpty()){


回答3:

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.
}


回答4:

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

editText.setHint("0");


回答5:

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!!!}