简体   繁体   中英

App crashes when attempting to parse integer from EditText

I'm a beginner to programming and am working on some small project. However, I'm stuck in a place and I don't know how to tackle this.

floatingActionButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view)
        {
            String inter = editText1.getText().toString();
            String interT = editText2.getText().toString();
            String NAT = editText3.getText().toString();
            String skip = editText4.getText().toString();

            if (TextUtils.isEmpty(inter) && TextUtils.isEmpty(interT) && TextUtils.isEmpty(NAT))
            {
                editText1.setError("Please Fill The Field");
                editText2.setError("Please Fill The Field");
                editText3.setError("Please Fill The Field");
            }

            else
                {


                        int INTER = Integer.valueOf(editText1.getText().toString());
                        int INTER_T = Integer.valueOf(editText2.getText().toString());
                        int NATT = Integer.valueOf(editText3.getText().toString());
                        int SKIP = Integer.valueOf(editText4.getText().toString());

                        float result = (INTER / INTER_T) * 80 + (NATT / 100) * 20;

                        t.setText(String.valueOf(result));
                    }


        }
    });

Here's the error:

java.lang.NumberFormatException: Invalid int: ""
at java.lang.Integer.invalidInt(Integer.java:138)
at java.lang.Integer.parseInt(Integer.java:358)
at java.lang.Integer.parseInt(Integer.java:334)
at java.lang.Integer.valueOf(Integer.java:525)
at com.example.iubmeritcalculator.BS_ProgramActivity$1.onClick(BS_ProgramActivity.java:70)
at android.view.View.performClick(View.java:5052)
at android.view.View$PerformClick.run(View.java:20162)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5753)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)

in condition you must have to check the value of skip "skip" wether it is null or not. App crash because "inter" "interT" and "NAT" value is not null but when "skip" value is null you are converting empty string to int.

if(TextUtils.isEmpty(inter) && TextUtils.isEmpty(interT) && TextUtils.isEmpty(NAT) && TextUtils.isEmpty(skip))

so i think this will fix your issue. Thanks.

I think this condition is wrong. To be true all of 3 conditions must be true.

if (TextUtils.isEmpty(inter) && TextUtils.isEmpty(interT) && TextUtils.isEmpty(NAT))

So if one of them is empty is it goes to else branch your app crashes because of the one of them is empty.

    if (!TextUtils.isEmpty(inter) && !TextUtils.isEmpty(interT) && !TextUtils.isEmpty(NAT)) {
        int INTER = Integer.valueOf(editText1.getText().toString());
        int INTER_T = Integer.valueOf(editText2.getText().toString());
        int NATT = Integer.valueOf(editText3.getText().toString());
        int SKIP = Integer.valueOf(editText4.getText().toString());

        float result = (INTER / INTER_T) * 80 + (NATT / 100) * 20;

        t.setText(String.valueOf(result));
    } else if (TextUtils.isEmpty(inter)) {
        editText1.setError("Please Fill The Field");
    } else if (TextUtils.isEmpty(interT)) {
        editText2.setError("Please Fill The Field");
    } else if (TextUtils.isEmpty(NAT)) {
        editText3.setError("Please Fill The Field");
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM