top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Error in Android java.lang.StackOverflowError

+5 votes
513 views

I have used below code,

textareaA.addTextChangedListener(new TextWatcher() {
    @Override
    public void afterTextChanged(Editable arg0) {
    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
    }

    @Override
    public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        double val = Double.parseDouble(textareaA.getText().toString());
        textareaB.setText(String.valueOf(val/10000));
    }      
});

textareaB.addTextChangedListener(new TextWatcher() {
    @Override
    public void afterTextChanged(Editable arg0) {
    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
    }

    @Override
    public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        double val = Double.parseDouble(textareaB.getText().toString());
       textareaA.setText(String.valueOf(val*10000));
    }      
});

If I type a value in any EditTexts, it crashes and trows java.lang.StackOverflowError error.
Suggest a solution.

posted Jun 3, 2016 by Shivam

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Yes, the above scenario definitely leads to deadlock. Let me explain how,

Your implementation,

textareaA -> onTextChanged -> set some value to textareaB
textareaB -> onTextChanged -> set some value to textareaA

Now, you make a change to any of the text boxes. For now say, textareaA it changes value in textareaB, which in-turn changes the value in textareaA, which in-turns change value of textareaB.... and so the loop goes on.

Possible solution is to check for the focused textbox.
That is,

textareaA -> onTextChanged -> set some value to textareaB, only if textareaB is not focused
textareaB -> onTextChanged -> set some value to textareaA, only if textareaA is not focused

Doing so should help you. Hope this helps.

answer Jun 14, 2016 by Vinod Kumar K V
Similar Questions
0 votes

I run my code in Eclipse IDE and its fine. I have export it as jar file from Eclipse and run on terminal as jar file, it generates Error: java.lang.NullPointerException. Could not understand why is it so.

+3 votes

I am receiving "Fatal error", if I change the layout.xml. It is that I am just swapping two elements, before swapping they work fine. After swapping, I am facing error. Help!

...