Reputation: 1838
I want to add to EditText
few ClickableSpan
in AsynkTask
.
I used the next code for this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
et = (EditText)findViewById(R.id.et);
...
class makeLinksAsync extends AsyncTask<String, String, EditText> {
private EditText buffer;
protected EditText doInBackground(String... texts) {
buffer = new EditText(context); // here is an error
SpannableString spanStr = new SpannableString("word");
...
buffer.append(spanStr);
return buffer;
}
protected void onPostExecute(EditText linkedText) {
ed.setText(linkedText.getText());
}
}
}
When I tested this code in different emulators and on my own device with Android2.3 everything was fine and this code worked well. But after uploaded apk to the GooglePlay I obtained few crash reports with bugs in mentioned line. Log report is the next:
java.lang.RuntimeException: An error occured while executing doInBackground()
...
Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
...
at lubart.apps.dictionary.DictionaryActivity$makeLinksAsync.doInBackground(DictionaryActivity.java:2233) // this line is mentioned in code
Also I should say, that this problem appear not in all devices, some users reports that everything work fine.
Can you help me with with this error?
Upvotes: 0
Views: 880
Reputation: 2186
This is my answer:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
et = (EditText)findViewById(R.id.et);
...
class makeLinksAsync extends AsyncTask<String, String, SpannableStringBuilder> {
protected SpannableStringBuilder doInBackground(String... texts) {
SpannableStringBuilder buffer = new SpannableStringBuilder()
...
buffer.append("word").append(...)...;
return buffer;
}
protected void onPostExecute(SpannableStringBuilder linkedText) {
ed.setText(linkedText);
}
}
}
Upvotes: 1
Reputation: 18670
You should not manipulate UI components (such as EditText
) in a background thread.
Just make sure you do not interact with any EditText
in your doInBackground
method.
Upvotes: 2