1

I'm making an android program and it's failing due to a stackoverflow error. I have attached my code. I'm using android studio. I believe that it's looping because of addTextChangedListener but I can't understand why it's doing it.

My Code:

    rb1 = (RadioButton) findViewById(R.id.radioButtonCFM);
    rb2 = (RadioButton) findViewById(R.id.radioButtonAC);

    rb1.setOnClickListener(new RadioGroup.OnClickListener() {
         public void onClick(View v) {
             EditText e1 = (EditText) findViewById(R.id.editText);
             String sUsername1 = e1.getText().toString();
             if (TextUtils.isEmpty(sUsername1)) {
                 e1.setError("The item cannot be empty.");
                 return;
             }

             EditText e2 = (EditText) findViewById(R.id.editText2);
             String sUsername2 = e2.getText().toString();
             if (TextUtils.isEmpty(sUsername2)) {
                 e2.setError("The item cannot be empty.");
                 return;
             }

             EditText e3 = (EditText) findViewById(R.id.editText3);
             String sUsername3 = e3.getText().toString();
             if (TextUtils.isEmpty(sUsername3)) {
                 e3.setError("The item cannot be empty.");
                 return;
             }

             e1.addTextChangedListener(new TextWatcher() {

                 public void afterTextChanged(Editable s) {
                 }

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

                 public void onTextChanged(CharSequence s, int start,
                                           int before, int count) {
                     EditText e1 = (EditText)findViewById(R.id.editText);
                     EditText e2 = (EditText)findViewById(R.id.editText2);
                     EditText e3 = (EditText)findViewById(R.id.editText3);
                     volume = Double.parseDouble(e1.getText().toString());
                     cfm = Double.parseDouble(e2.getText().toString());
                     ac = Double.parseDouble(e3.getText().toString());
                     TextView t1 = (TextView) findViewById(R.id.editText3);
                     volume = Double.parseDouble(e1.getText().toString());
                     cfm = Double.parseDouble(e2.getText().toString());
                     ac = cfm * 60 / volume;
                     t1.setText(Double.toString((double) Math.round(ac * 100000) / 100000));
                 }
             });


             e2.addTextChangedListener(new TextWatcher() {

                 public void afterTextChanged(Editable s) {
                 }

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

                 public void onTextChanged(CharSequence s, int start,
                                           int before, int count) {
                     EditText e1 = (EditText)findViewById(R.id.editText);
                     EditText e2 = (EditText)findViewById(R.id.editText2);
                     EditText e3 = (EditText)findViewById(R.id.editText3);
                     volume = Double.parseDouble(e1.getText().toString());
                     cfm = Double.parseDouble(e2.getText().toString());
                     ac = Double.parseDouble(e3.getText().toString());
                     TextView t1 = (TextView) findViewById(R.id.editText3);
                     volume = Double.parseDouble(e1.getText().toString());
                     cfm = Double.parseDouble(e2.getText().toString());
                     ac = cfm * 60 / volume;
                     t1.setText(Double.toString((double) Math.round(ac * 100000) / 100000));
                 }
             });

         }});

     rb2.setOnClickListener(new RadioGroup.OnClickListener() {
         public void onClick(View v) {
             EditText e1 = (EditText)findViewById(R.id.editText);
             String sUsername1 = e1.getText().toString();
             if(TextUtils.isEmpty(sUsername1)) {
                 e1.setError("The item cannot be empty.");
                 return;
             }

             EditText e2 = (EditText)findViewById(R.id.editText2);
             String sUsername2 = e2.getText().toString();
             if(TextUtils.isEmpty(sUsername2)) {
                 e2.setError("The item cannot be empty.");
                 return;
             }

             EditText e3 = (EditText)findViewById(R.id.editText3);
             String sUsername3 = e3.getText().toString();
             if(TextUtils.isEmpty(sUsername3)) {
                 e3.setError("The item cannot be empty.");
                 return;
             }

             e1.addTextChangedListener(new TextWatcher() {

                 public void afterTextChanged(Editable s) {
                 }

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

                 public void onTextChanged(CharSequence s, int start,
                                           int before, int count) {
                     EditText e1 = (EditText)findViewById(R.id.editText);
                     EditText e2 = (EditText)findViewById(R.id.editText2);
                     EditText e3 = (EditText)findViewById(R.id.editText3);
                     volume = Double.parseDouble(e1.getText().toString());
                     cfm = Double.parseDouble(e2.getText().toString());
                     ac = Double.parseDouble(e3.getText().toString());

                     TextView t2 = (TextView)findViewById(R.id.editText2);
                     volume = Double.parseDouble(e1.getText().toString());
                     ac = Double.parseDouble(e3.getText().toString());
                     cfm = ac * volume / 60;
                     t2.setText(Double.toString((double) Math.round(cfm * 100000) / 100000));
                 }
             });

             e3.addTextChangedListener(new TextWatcher() {

                 public void afterTextChanged(Editable s) {
                 }

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

                 public void onTextChanged(CharSequence s, int start,
                                           int before, int count) {
                     EditText e1 = (EditText)findViewById(R.id.editText);
                     EditText e2 = (EditText)findViewById(R.id.editText2);
                     EditText e3 = (EditText)findViewById(R.id.editText3);
                     volume = Double.parseDouble(e1.getText().toString());
                     cfm = Double.parseDouble(e2.getText().toString());
                     ac = Double.parseDouble(e3.getText().toString());

                     TextView t2 = (TextView)findViewById(R.id.editText2);
                     volume = Double.parseDouble(e1.getText().toString());
                     ac = Double.parseDouble(e3.getText().toString());
                     cfm = ac * volume / 60;
                     t2.setText(Double.toString((double) Math.round(cfm * 100000) / 100000));
                 }
             });

         }
     });
     }
fadden
  • 51,356
  • 5
  • 116
  • 166
  • Use LogCat to examine the Java stack trace to see exactly where you are having infinite recursion: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare Mar 16 '15 at 23:47
  • I tried to examine it and I can tell that its repeating. But I can't understand much of it unfortunately. How do you post the logcat on here? it's too long. – Four Loko Breakfast Mar 17 '15 at 08:58

3 Answers3

1

Your TextWatcher that's watching the input of R.id.editText3 is modifying the text of R.id.editText2, which is in turn modifying R.id.editText3. Through that, you'll get infinite calls to onTextChanged(...).

  • But I have rb1.setOnClickListener and rb2.setOnClickListener which separates the listeners. Its suppose to change R.id.editText3 based on R.id.editText1 & R.id.editText2 when rb1.setOnClickListener is clicked. And its suppose to change R.id.editText2 based on R.id.editText1 & R.id.editText3 when rb2.setOnClickListener is clicked. So is it still listening to R.id.editText3 even tho rb1.setOnClickListener is clicked? How should I get around this and make it not listen? – Four Loko Breakfast Mar 17 '15 at 08:47
0

Try adding this line on your activity :

@Override

public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if ((keyCode == KeyEvent.KEYCODE_BACK))
        {

            finish();

        }
        return super.onKeyDown(keyCode, event);
    }
Tony
  • 2,515
  • 14
  • 38
  • 71
UserSS
  • 23
  • 7
  • I copy and pasted it into my main activity. However, it did not solve java.lang.StackOverflowError. What is this suppose to do and how come your not linking it to texboxes? It doesn't seem to do much. – Four Loko Breakfast Mar 17 '15 at 08:33
0

You're calling findViewById() repeatedly. Just make all the Views fields and "find" them in onCreate() after setContentView().

I notice you call

TextView t1 = (TextView) findViewById(R.id.edittext3);

There's no need to have duplicate pointers to the same View. Simplify your code and make sure all the variables point to the correct target and that the TextWatchers don't trigger themselves.

Fletcher Johns
  • 1,236
  • 15
  • 20