AsyncTask in custom EditText
<p>So I'm a bit puzzled. I'm using the TextWatcher afterTextChanged(Editable s) method to search thru the text that the user enters and detect if he typed one of the word of a list and if yes to add a span to that position.</p>

<p>When I tried to implement it directly in the afterTextChanged, as the text grows it harder to search it and span the specific words, so I moved the work on an AsynkTask class. Here is the class:</p>

<pre><code> class SearchText extends AsyncTask&lt;String, String, Void&gt; {
    private String match = "(\\d )?(([\\p{Alnum}\\p{Punct}])+?) ([0-9]+?)\\b:([0-9]+?)(, ([0-9]+?))?(-([0-9]+?))?\\b";
    private Editable s;

    SearchText(Editable s) {
        this.s = s;
    }

    @Override
    protected Void doInBackground(String... params) {
        Matcher m = Pattern.compile(match).matcher(s);
        while (m.find()) {
            Reference reference = new Reference(m.group(9));
            if (booksList.contains(m.group(2)) &amp;&amp; mListener.check(reference)) {

                BooksSpan booksSpan = new BooksSpan("1");
                try {
                    s.setSpan(booksSpan, m.start(), m.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                } catch (Exception e) {
                    Log.e("error", e.getMessage());
                }
            }
        }
        return null;
    }
}
</code></pre>

<p><code>s.setSpan(booksSpan, m.start(), m.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);</code> when I type really fast, like random words, it gives me:</p>

<p><code>Only the original thread that created a view hierarchy can touch its views.</code></p>

<p>Why is this just sometimes? Editable is not a view. What am I missing? </p>