0

Calling setText("") app getting crashed or How can i set CharSequence NULL value. Whenever i have only one character in the EditText and using Key Stroke backspace my app getting crashed.

I have an EditText with below code:

        bid_four = findViewById(R.id.bid_four);
        bid_four.setFilters(new InputFilter[]{ new InputFilterMinMax("1", "1000")});
        bid_four.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                bid_4= Integer.parseInt(String.valueOf(s));
                total_ticket = (bid_0 + bid_1 + bid_2 + bid_3 + bid_4 + bid_5 + bid_6 + bid_7 + bid_8 + bid_9);
                total_value = (total_ticket * 11);
                total_number_of_ticket.setText(Integer.toString(total_ticket));
                total_number_of_purchase_ticket.setText(Integer.toString(total_value));
                buy_button_enable();
            }
        });
Saurabh
  • 1
  • 2
  • What is the error message? – aiqency Mar 20 '20 at 20:35
  • android.content.res.Resources$NotFoundException: String resource ID #0x0 at android.content.res.Resources.getText(Resources.java:360) at android.content.res.MiuiResources.getText(MiuiResources.java:97) at android.widget.TextView.setText(TextView.java:5856) – Saurabh Mar 20 '20 at 20:39
  • com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:78) at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30) at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:106) at android.os.Handler.handleCallback(Handler.java:873) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:201) at android.app.ActivityThread.main(ActivityThread.java:6810) at java.lang.reflect.Method.invoke(Native Method) at – Saurabh Mar 20 '20 at 20:40
  • 1
    Please Post it on your original post – aiqency Mar 20 '20 at 20:41
  • 1
    It say that your are calling getText() somewhere with a resource that do not exist. If you want further help please post more code. Maybe the buy_button_enable method – aiqency Mar 20 '20 at 20:43
  • Problem is not on buy_button_enable. Whenever EditText have only one character and if at that time i am using backspace at that time my app getting crached – Saurabh Mar 20 '20 at 20:46
  • 1
    As @aiqency said, the buggy code isn't in what you included. See [What is a stack trace, and how can I use it to debug my application errors?](https://stackoverflow.com/questions/3988788) for advice on how to debug this using the stack trace. – Ryan M Mar 20 '20 at 20:49

1 Answers1

1

Whenever i have only one character in the EditText and using Key Stroke backspace my app getting crashed.

public void afterTextChanged(Editable s) {
    bid_4= Integer.parseInt(String.valueOf(s));

When there's a one character and you delete it, there aren't any characters left. You can't make integer out of an empty string.

I suggest treating empty string as zero, if that fits your case:

if (s.length() == 0) {
    bid_4 = 0;
} else {
    bid_4 = Integer.parseInt(s.toString());
}

The exception you posted in comments doesn't correspond to the issue described in your question. Anyway, don't call TextView.setText with an integer. The integer is supposed to be a resource ID, in your case you want do display a literal integer. Make it a string first:

String.valueOf(someNumber);
someNumber + "";

You did that correctly in the code sample so you must have made mistake somewhere else.

Eugen Pechanec
  • 37,669
  • 7
  • 103
  • 124