lundi 27 février 2017

App crashes repeatedly due to empty EditText

I have created a simple app to calculate the discount. it works fine when the values are provided, but keeps on crashing when calculation is done with empty text fields. thanks in advance..!!

public class Firstctivity extends AppCompatActivity {
   Button find;
   EditText ed1,ed2;
   TextView tv1;
   @Override
   protected void onCreate(Bundle savedInstanceState)
   {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_firstctivity);
    find=(Button)findViewById(R.id.button);
    tv1=(TextView)findViewById(R.id.text39);
    ed1=(EditText)findViewById(R.id.sai);
    ed2=(EditText)findViewById(R.id.editText);


    find.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {

            final double a =Double.parseDouble(String.valueOf(ed1.getText()));
            final double b =Double.parseDouble(String.valueOf(ed2.getText()));
            double results;
            double c;
           try {
               c = a * b;
               results = c / 100;
               String total2 = String.valueOf(results);
               // String total2="fuck u";
               tv1.setText("You have recieved Rs" + total2 + "/- concession.");
               tv1.setBackgroundColor(Color.parseColor("#4169E1"));
               }
          catch (NumberFormatException ex)

           {

           }


        }

    });


}

}

Aucun commentaire:

Enregistrer un commentaire