私はちょっと困惑しています。私はTextWatcher afterTextChanged(Editable s)メソッドを使用して、ユーザーが入力したテキストを検索し、リストの単語の1つを入力した場合は検出し、そうであればその位置にスパンを追加します。AsyncTask in Custom EditText
私がAfterTextChangedで直接実装しようとしたとき、テキストが拡大して特定の単語にまたがることが困難になり、AsynkTaskクラスで作業を移動しました。ここではクラスがある:
class SearchText extends AsyncTask<String, String, Void> {
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)) && 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;
}
}
s.setSpan(booksSpan, m.start(), m.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
私は本当に速く入力すると、ランダムな言葉のように、それは私を与える:
Only the original thread that created a view hierarchy can touch its views.
なぜこれがちょうど時々ありますか?編集可能なのはビューではありません。私は何が欠けていますか?
なぜこのようなことが起こるのかわかりません。しかし、AsyncTaskを使うときは、UIを操作するために 'onPostExecute()'を使うべきです。 – AlbAtNf
編集可能なUIの仕組みは? –
完全なスタックトレースを表示します。 –