Application closes at the touch of the button

Asked

Viewed 86 times

1

When one or more EditTextare empty the tap on the button should show the message of the Toast, but the application closes. Why?

public class HomeActivity extends Activity {

    int rcpNum, fckNum, abtmNum, mucBritaNum, meCimentoNum, meBritaNum, meAreiaNum;
    double dmaxNum, mfNum, sdNum;

    EditText rcpTxt, fckTxt, abtmTxt, dmaxTxt, mfTxt, mucBritaTxt, meCimentoTxt, meBritaTxt, meAreiaTxt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_avancado);

        rcpTxt = (EditText) findViewById(R.id.rcp_edt);
        fckTxt = (EditText) findViewById(R.id.fck_edt);
        abtmTxt = (EditText) findViewById(R.id.abtm_edt);
        dmaxTxt = (EditText) findViewById(R.id.dmax_edt);
        mfTxt = (EditText) findViewById(R.id.mf_edt);

        mucBritaTxt = (EditText) findViewById(R.id.mucbrita_edt);
        meCimentoTxt = (EditText) findViewById(R.id.mecimento_edt);
        meBritaTxt = (EditText) findViewById(R.id.mebrita_edt);
        meAreiaTxt = (EditText) findViewById(R.id.meareia_edt);

        //Spinner
        Spinner spnSd = (Spinner) findViewById(R.id.spn_sd);

        ArrayAdapter<CharSequence> spnAdapter = ArrayAdapter.createFromResource(this, R.array.spn_sd_str, R.layout.spinner_style);
        spnAdapter.setDropDownViewResource(R.layout.spinner_dropdown_style);

        spnSd.setAdapter(spnAdapter);

        spnSd.setOnItemSelectedListener(
                new AdapterView.OnItemSelectedListener() {

                    public void onItemSelected(AdapterView<?> spnAdpView, View v, int sdPosition, long id) {

                        if(spnAdpView.getItemAtPosition(sdPosition).toString().equals("SD = 4,5 MPa")){ sdNum = 4.5; }
                        else if(spnAdpView.getItemAtPosition(sdPosition).toString().equals("SD = 5,5 MPa")){ sdNum = 5.5; }
                        else { sdNum = 7.5; }

                    }// fecha onItemSelected

                    public void onNothingSelected(AdapterView<?> arg0){}
                }//fecha OnItemSelectedListener
        ); // fecha setOnItemSelectedListener
        //spinner



        // CALCULAR #######################################
                ImageButton calcTraco = (ImageButton) findViewById(R.id.btn_traco);
                calcTraco.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        rcpNum = Integer.parseInt(rcpTxt.getText().toString());
                        fckNum = Integer.parseInt(fckTxt.getText().toString());
                        abtmNum = Integer.parseInt(abtmTxt.getText().toString());
                        mucBritaNum = Integer.parseInt(mucBritaTxt.getText().toString());
                        meCimentoNum = Integer.parseInt(meCimentoTxt.getText().toString());
                        meBritaNum = Integer.parseInt(meBritaTxt.getText().toString());
                        meAreiaNum = Integer.parseInt(meAreiaTxt.getText().toString());

                        dmaxNum = Double.parseDouble(dmaxTxt.getText().toString());
                        mfNum = Double.parseDouble(mfTxt.getText().toString());

                        if(rcpNum == 0 ||
                           fckNum == 0 ||
                           abtmNum == 0 ||
                           mucBritaNum == 0 ||
                           meCimentoNum == 0 ||
                           meBritaNum == 0 ||
                           meAreiaNum == 0 ||
                           dmaxNum == 0 ||
                           mfNum == 0){
                            Toast.makeText(getApplicationContext(), "Preencha todos os campos!", Toast.LENGTH_SHORT).show();
                        }
                        else {
                            Intent intentTraco = new Intent(v.getContext(), TracoActivity.class);
                            intentTraco.putExtra("rcpNum", (int)rcpNum);
                            intentTraco.putExtra("fckNum", (int)fckNum);
                            intentTraco.putExtra("abtmNum", (int)abtmNum);
                            intentTraco.putExtra("mucBritaNum", (int)mucBritaNum);
                            intentTraco.putExtra("meCimentoNum", (int)meCimentoNum);
                            intentTraco.putExtra("meBritaNum", (int)meBritaNum);
                            intentTraco.putExtra("meAreiaNum", (int)meAreiaNum);

                            intentTraco.putExtra("dmaxNum", (double)dmaxNum);
                            intentTraco.putExtra("mfNum", (double)mfNum);
                            intentTraco.putExtra("sdNum", (double)sdNum);
                            startActivityForResult(intentTraco, 0);
                         }
                    }
                });
                // CALCULAR #######################################

        } //fecha onCreate
    }

Follow the Logcat:

12-14 20:47:40.367: E/AndroidRuntime(18566): FATAL EXCEPTION: main
12-14 20:47:40.367: E/AndroidRuntime(18566): Process: com.edeiltonso.dosapp, PID: 18566
12-14 20:47:40.367: E/AndroidRuntime(18566): java.lang.NumberFormatException: Invalid int: ""
12-14 20:47:40.367: E/AndroidRuntime(18566):    at java.lang.Integer.invalidInt(Integer.java:137)
12-14 20:47:40.367: E/AndroidRuntime(18566):    at java.lang.Integer.parseInt(Integer.java:358)
12-14 20:47:40.367: E/AndroidRuntime(18566):    at java.lang.Integer.parseInt(Integer.java:331)
12-14 20:47:40.367: E/AndroidRuntime(18566):    at com.edeiltonso.dosapp.HomeActivity$2.onClick(HomeActivity.java:70)
12-14 20:47:40.367: E/AndroidRuntime(18566):    at android.view.View.performClick(View.java:4445)
12-14 20:47:40.367: E/AndroidRuntime(18566):    at android.view.View$PerformClick.run(View.java:18446)
12-14 20:47:40.367: E/AndroidRuntime(18566):    at android.os.Handler.handleCallback(Handler.java:733)
12-14 20:47:40.367: E/AndroidRuntime(18566):    at android.os.Handler.dispatchMessage(Handler.java:95)
12-14 20:47:40.367: E/AndroidRuntime(18566):    at android.os.Looper.loop(Looper.java:136)
12-14 20:47:40.367: E/AndroidRuntime(18566):    at android.app.ActivityThread.main(ActivityThread.java:5141)
12-14 20:47:40.367: E/AndroidRuntime(18566):    at java.lang.reflect.Method.invokeNative(Native Method)
12-14 20:47:40.367: E/AndroidRuntime(18566):    at java.lang.reflect.Method.invoke(Method.java:515)
12-14 20:47:40.367: E/AndroidRuntime(18566):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
12-14 20:47:40.367: E/AndroidRuntime(18566):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
12-14 20:47:40.367: E/AndroidRuntime(18566):    at dalvik.system.NativeStart.main(Native Method)

2 answers

4

Your mistake seems to be coming from these lines:

                    rcpNum = Integer.parseInt(rcpTxt.getText().toString());
                    fckNum = Integer.parseInt(fckTxt.getText().toString());
                    abtmNum = Integer.parseInt(abtmTxt.getText().toString());
                    mucBritaNum = Integer.parseInt(mucBritaTxt.getText().toString());
                    meCimentoNum = Integer.parseInt(meCimentoTxt.getText().toString());
                    meBritaNum = Integer.parseInt(meBritaTxt.getText().toString());
                    meAreiaNum = Integer.parseInt(meAreiaTxt.getText().toString());

Note the error:

java.lang.NumberFormatException: Invalid int: ""

That is, a String empty is being passed to one of the parseInts. This means that at least one of the EditTexts is blank. Nowhere are you validating whether they have been filled in and have been filled in correctly. And therefore, an error occurs when one of them is not properly filled in.

1


To validate whether the field is empty use the property length. The way you did, you are (was) comparing whether the EditText is equal to 0.


In your example it would look like this:

if(rcpNum.length() == 0 ||
   fckNum.length() == 0 ||
   abtmNum.length() == 0 ||
   mucBritaNum.length() == 0 ||
   meCimentoNum.length() == 0 ||
   meBritaNum.length() == 0 ||
   meAreiaNum.length() == 0 ||
   dmaxNum.length() == 0 ||
   mfNum.length() == 0){  // o resto do código ... }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.