'RichInputConnection: Unable to connect to the editor to retrieve text

I have a EditText, and I have a a TextWatcher set on it. The purpose of the TextWatcher is to edit another EditText after the first one is changed. This has been working before, but when I booted up Android studio today, whenever I edited the text, I get this in the logcat:

2022-04-19 14:44:25.733 16784-16784/com.android.inputmethod.latin I/LatinIME: Starting input. Cursor position = 0,0
2022-04-19 14:44:25.734 16784-16784/com.android.inputmethod.latin E/RichInputConnection: Unable to connect to the editor to retrieve text.
2022-04-19 14:44:25.734 16784-16784/com.android.inputmethod.latin D/RichInputConnection: Will try to retrieve text later.
2022-04-19 14:44:25.734 16784-16784/com.android.inputmethod.latin E/RichInputConnection: Unable to connect to the editor to retrieve text.
2022-04-19 14:44:25.734 16784-16784/com.android.inputmethod.latin W/RichInputConnection: Unable to connect to the editor. Setting caps mode without knowing text.
2022-04-19 14:44:25.735 16784-16784/com.android.inputmethod.latin E/RichInputConnection: Unable to connect to the editor to retrieve text.
2022-04-19 14:44:25.735 16784-16784/com.android.inputmethod.latin W/RichInputConnection: Unable to connect to the editor. Setting caps mode without knowing text.

Here's my code (excuse how bad it is, I'm going to optimise it):

public class TextWatcherEngine implements TextWatcher {
    private static boolean ackChange = false;
    private static ArrayList<TextWatcherEngine> INSTANCES = new ArrayList<>();
    private final String id;
    private final EditText textview;
    private final EditText othertext;
    private final Spinner otherSpinner;
    private final JSONArray rates;
    private final Spinner spinner;
    private String total;
    private CharSequence before;
    private boolean shouldChange = false;
    private boolean shouldUpdate = false;

    public TextWatcherEngine(String id, EditText textview, EditText othertext, JSONArray rates, Spinner spinner, Spinner otherSpinner) {
        this.textview = textview;
        this.othertext = othertext;
        this.rates = rates;
        this.id = id;
        this.spinner = spinner;
        this.otherSpinner = otherSpinner;
        INSTANCES.add(this);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        this.before = s;
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        update(s, true);
    }

    @Override
    public void afterTextChanged(Editable s) {
        if (shouldChange){
            textview.setText(before.toString());
            shouldChange = false;
        }
        if (shouldUpdate){
            othertext.setText(total);
            shouldUpdate = false;
        }
    }

    public String getId(){
        return this.id;
    }

    public static TextWatcherEngine findInstance(String id){
        for (TextWatcherEngine i : INSTANCES){
            if (i.getId().equals(id)){
                return i;
            }
        }
        return null;
    }

    public void update(CharSequence s, boolean fromInside){
        if (s.length() == 0) return;
        if (ackChange){
            ackChange = false;
            return;
        }
        //check for any non digit characters
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (!Character.isDigit(c) && c != '.'){
                shouldChange = true;
                return;
            }
        }

        double amount = Double.parseDouble(s.toString());
        String selected = (String) spinner.getSelectedItem();
        String conversion = (String) otherSpinner.getSelectedItem();
        JSONObject rateTable = null;

        try{
            for (int i = 0; i < rates.length(); i++) {
                JSONObject current = rates.getJSONObject(i);
                if (Double.parseDouble(current.get(selected).toString()) == 1){
                    rateTable = current;
                    break;
                }
            }

            if (rateTable == null) {
                return;
            }

            if (fromInside){
                total = String.format(Locale.US, "%.2f", Math.floor((rateTable.getDouble(conversion) * amount * 100)) / 100);
                shouldUpdate = true;
                ackChange = true;
                othertext.setText(total);
            } else {
                total = String.format(Locale.US, "%.2f", Math.floor((rateTable.getDouble(selected) * amount * 100)) / 100);
                ackChange = true;
                othertext.setText(total);
            }
        } catch (JSONException e){
            e.printStackTrace();
        }
    }

    public EditText getTextView(){
        return textview;
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source