Search code examples
androidmobileandroid-edittextstack-overflow

StackOverFlowError in EditText android


I'm trying to use mask for two Fields and When i use a mask for editText the app crash. Here is the image of error.

enter image description here

Here is the Mask that i use for like listener of EditText

public abstract class Mask {

public static String unmask(String s) {
    return s.replaceAll("[.]", "").replaceAll("[-]", "")
            .replaceAll("[/]", "").replaceAll("[(]", "")
            .replaceAll("[)]", "");
}

public static String maskToWebService(String s) {
    return s.replace("/", "T");
}

public static TextWatcher insert(final String mask, final EditText ediTxt) {
    return new TextWatcher() {
        boolean isUpdating;
        String old = "";

        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            String str = Mask.unmask(s.toString());
            String mascara = "";
            if (isUpdating) {
                old = str;
                isUpdating = false;
                return;
            }
            int i = 0;
            for (char m : mask.toCharArray()) {
                if (m != '#' && str.length() > old.length()) {
                    mascara += m;
                    continue;
                }
                try {
                    mascara += str.charAt(i);
                } catch (Exception e) {
                    break;
                }
                i++;
            }
            isUpdating = true;
            ediTxt.setText(mascara);
            ediTxt.setSelection(mascara.length());
        }

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

        public void afterTextChanged(Editable s) {

        }
    };
}

How can resolve?


Solution

  • I found the problem. The TextWatcher is being calling many times to a instance