更改editText时StackOverFlow

时间:2014-10-28 17:20:37

标签: android stack-overflow

我有三个EditText。我想听取每个输入的变化,并在给定输入值的情况下自动更改它们。但是当有变化时,听众递归地开始互相改变,我抓住了java.lang.StackOverFlow。我该怎么办?

我在 afterTextChanged(可编辑的)中对ediText进行了更改。

priceEditText.addTextChangedListener(new TextWatcher() {
                public void afterTextChanged(Editable s) {
                    try {
                        if (priceForLiterEditText.getText().length() > 0 && isPriceFocused) {
                            double summ = Double.valueOf(priceEditText.getText().toString());
                            double literPrice = Double.valueOf(priceForLiterEditText.getText().toString());
                            summ = summ / literPrice;
                            summ = Math.round(summ * 100);
                            summ /= 100;
                            fuelLitersAmountEditText.setText(String.valueOf(summ));
                            fuelLitersAmountTextView.setText(String.valueOf(summ) + " " + currentCar.getCurrencyName(getApplicationContext()));
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

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

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

ATAL EXCEPTION:主要       java.lang.StackOverflowError的               在android.graphics.Paint.set(Paint.java:481)               在android.text.TextPaint.set(TextPaint.java:61)               在android.text.TextLine.handleRun(TextLine.java:1091)               在android.text.TextLine.measureRun(TextLine.java:599)               在android.text.TextLine.measure(TextLine.java:462)               在android.text.Layout.getHorizo​​ntal(Layout.java:852)               在android.text.Layout.getHorizo​​ntal(Layout.java:829)               在android.text.Layout.getPrimaryHorizo​​ntal(Layout.java:813)               在android.widget.TextView.getFocusedRect(TextView.java:5602)               在android.view.FocusFinder.findNextFocusInAbsoluteDirection(FocusFinder.java:231)               在android.view.FocusFinder.findNextFocus(FocusFinder.java:163)               在android.view.FocusFinder.findNextFocus(FocusFinder.java:94)               在android.view.FocusFinder.findNextFocus(FocusFinder.java:65)               在android.view.ViewGroup.focusSearch(ViewGroup.java:680)               在android.view.ViewGroup.focusSearch(ViewGroup.java:682)               在android.view.ViewGroup.focusSearch(ViewGroup.java:682)               在android.view.ViewGroup.focusSearch(ViewGroup.java:682)               在android.view.ViewGroup.focusSearch(ViewGroup.java:682)               在android.view.ViewGroup.focusSearch(ViewGroup.java:682)               在android.view.ViewGroup.focusSearch(ViewGroup.java:682)               在android.view.ViewGroup.focusSearch(ViewGroup.java:682)               在android.view.ViewGroup.focusSearch(ViewGroup.java:682)               在android.view.ViewGroup.focusSearch(ViewGroup.java:682)               在android.view.View.focusSearch(View.java:6456)               在android.widget.TextView.onCreateInputConnection(TextView.java:6245)               在android.view.inputmethod.InputMethodManager.startInputInner(InputMethodManager.java:1168)               在android.view.inputmethod.InputMethodManager.restartInput(InputMethodManager.java:1119)               在android.widget.TextView.setText(TextView.java:4019)               在android.widget.TextView.setText(TextView.java:3927)               在android.widget.EditText.setText(EditText.java:104)               在android.widget.TextView.setText(TextView.java:3902)               at com.DriverNotes.AndroidMobileClientTest.RefillActivity $ 9 $ 2.afterTextChanged(RefillActivity.java:866)               在android.widget.TextView.sendAfterTextChanged(TextView.java:7986)               在android.widget.TextView.setText(TextView.java:4114)               在android.widget.TextView.setText(TextView.java:3927)               在android.widget.EditText.setText(EditText.java:104)               在android.widget.TextView.setText(TextView.java:3902)               at com.DriverNotes.AndroidMobileClientTest.RefillActivity $ 11 $ 2.afterTextChanged(RefillActivity.java:1012)               在android.widget.TextView.sendAfterTextChanged(TextView.java:7986)               在android.widget.TextView.setText(TextView.java:4114)               在android.widget.TextView.setText(TextView.java:3927)               在android.widget.EditText.setText(EditText.java:104)               在android.widget.TextView.setText(TextView.java:3902)               at com.DriverNotes.AndroidMobileClientTest.RefillActivity $ 9 $ 2.afterTextChanged(RefillActivity.java:866)               在android.widget.TextView.sendAfterTextChanged(TextView.java:7986)               在android.widget.TextView.setText(TextView.java:4114)               在android.widget.TextView.setText(TextView.java:3927)               在android.widget.EditText.setText(EditText.java:104)               在android.widget.TextView.setText(TextView.java:3902)               at com.DriverNotes.AndroidMobileClientTest.RefillActivity $ 11 $ 2.afterTextChanged(RefillActivity.java:1012)               在android.widget.TextView.sendAfterTextChanged(TextView.java:7986)               在android.widget.TextView.setText(TextView.java:4114)               在android.widget.TextView.setText(TextView.java:3927)               在android.widget.EditText.setText(EditText.java:104)               在android.widget.TextView.setText(TextView.java:3902)               at com.DriverNotes.AndroidMobileClientTest.RefillActivity $ 9 $ 2.afterTextChanged(RefillActivity.java:866)               在android.widget.TextView.sendAfterTextChanged(TextView.java:7986)               在android.widget.TextView.setText(TextView.java:4114)               在android.widget.TextView.setText(TextView.java:3927)               在android.widget.EditText.setText(EditText.java:104)               在android.widget.TextView.setText(TextView.java:3902)               在com.DriverNotes.AndroidMobileClientTest.RefillActivity $ 11 $ 2.afterTextChanged(RefillActivity.java:1012)

1 个答案:

答案 0 :(得分:1)

你有TextChangedListeners互相触发。您应该添加一个检查,仅在用户输入指向的任何字段上执行此操作。

com.DriverNotes.AndroidMobileClientTest.RefillActivity$11$2.afterTextChanged(RefillActivity.java:1012)
com.DriverNotes.AndroidMobileClientTest.RefillActivity$9$2.afterTextChanged(RefillActivity.java:866)
com.DriverNotes.AndroidMobileClientTest.RefillActivity$11$2.afterTextChanged(RefillActivity.java:1012)
com.DriverNotes.AndroidMobileClientTest.RefillActivity$9$2.afterTextChanged(RefillActivity.java:866)
...