0

In multiple edittext in textwatcher, it works well if some value is added at all. Now if I erase something from one of them then it crashes.

In Multiple EditText in TextWatcher, it works well when there is some value added.

If I don't erase any of them then it's working fine.

Please explain me where I am making a mistake.

private final TextWatcher rateValue = new TextWatcher() {

    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
    public void onTextChanged(CharSequence s, int start, int before, int count) {}

    public void afterTextChanged(Editable s) {
        String rate = item_sale_rate.getText().toString();
        Double sellRateInNo = Double.valueOf(rate); // String to Integer
        s_mrp_value.setText(item_sale_rate.getText());
        s_rate_value.setText(item_sale_rate.getText());
    }
};

private final TextWatcher discountValue = new TextWatcher() {
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
    public void onTextChanged(CharSequence s, int start, int before, int count) {}

    public void afterTextChanged(Editable s) {

        String rate = item_sale_rate.getText().toString();
        String discount = item_sale_discount.getText().toString();

        int sellRateInNo = Integer.valueOf(rate); // String to Integer
        int sellDisInNo = Integer.valueOf(discount); // String to Integer

        s_rate_value.setText(item_sale_rate.getText().toString()); // at is String
        s_discount_value.setText(item_sale_discount.getText().toString()); // at is String

        int taxableInNo = sellRateInNo - sellDisInNo; // calculation
        s_taxable_value.setText(String.valueOf(taxableInNo)); // Integer to String
    }
};

private final TextWatcher taxValue = new TextWatcher() {
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
    public void onTextChanged(CharSequence s, int start, int before, int count) {}

    public void afterTextChanged(Editable s) {

        String rate = item_sale_rate.getText().toString();
        String discount = item_sale_discount.getText().toString();
        String tax = item_sale_tax.getText().toString();

        int sellRateInNo = Integer.valueOf(rate); // String to Integer
        int sellDisInNo = Integer.valueOf(discount); // String to Integer
        int sellTaxInNo = Integer.valueOf(tax); // String to Integer

        s_rate_value.setText(item_sale_rate.getText().toString()); // at is String
        s_discount_value.setText(item_sale_discount.getText().toString()); // at is String

        int taxableInNo = sellRateInNo - sellDisInNo; // calculation
        s_taxable_value.setText(String.valueOf(taxableInNo)); // Integer to String

        s_total_value.setText(String.valueOf(priceInNo));
    }
};
barbsan
  • 3,418
  • 11
  • 21
  • 28
Shivendra Kr. Sahu
  • 753
  • 1
  • 5
  • 12
  • 1
    Whenever you post a question about a crash, please provide the complete [stack trace](https://stackoverflow.com/a/23353174). – Mike M. Aug 08 '19 at 22:57
  • how can use debug , that's i dont know , and my has dual core there are no any option for **Hardware Acceleration on** – Shivendra Kr. Sahu Aug 10 '19 at 08:06

1 Answers1

0

Since you havent posted any logs i'm going to guess that you might need to check each string for null or empty before attempting to convert it to a Double or Int (basically your strings are "") :

if(str != null && !str.isEmpty()) { 
    /* code */ 
}

Example

Double sellRateInNo = 0;
String rate = item_sale_rate.getText().toString();
If (rate != null && !rate.isEmpty()) {
     sellRateInNo = Double.valueOf(rate);
}
JakeB
  • 2,043
  • 3
  • 12
  • 19
  • Would you please give me some examples with my code.I'm beginner So I don't understand such a small hint. – Shivendra Kr. Sahu Aug 08 '19 at 20:09
  • Added example. Before you change the strings to another data type you need to ensure its in a state to be used, ie: not null or empty. – JakeB Aug 08 '19 at 20:18
  • you are tell like this if (s.length() > 0) { taxcalculat(); } else { disDisCalculate(); } ? – Shivendra Kr. Sahu Aug 08 '19 at 20:32
  • IsEmpty() will be sufficient. – JakeB Aug 08 '19 at 20:55
  • It is working properly, but as I delete something from tuxeditbox then my calculation is going wrong – Shivendra Kr. Sahu Aug 09 '19 at 08:11
  • It's calculating on each text change, so the returned calculation will resemble each state of each textbox when one changes. Maybe it would be better to have a button to make the calculation? – JakeB Aug 09 '19 at 11:30
  • When values are entered or changed in sequence that is Sale rate box , sale discount box and sale tax box , it is giving correct values but When any value of three variable is changed in reverse order or random boxes , calculation is disturbed. Please give suggestion for this with out using button. – Shivendra Kr. Sahu Aug 09 '19 at 17:41
  • You should have global variables for rate, discount, tax etc.. that are updated afterTextChanged() and then at the end of that, a new function is called, for example, "caltulate()". This function will take these global variabes and set the value to the "s_total_value" edittext. – JakeB Aug 09 '19 at 19:28
  • Like you explained earlier, please tell me in the same way. i request you – Shivendra Kr. Sahu Aug 09 '19 at 19:34
  • dear all its important for me . please guide me – Shivendra Kr. Sahu Aug 11 '19 at 18:35