Edittext Crashing On Clearing Text
Solution 1:
Problem is "updateSuggestions" try to setSpan
after you change text in EditText
.
So you need make text change after editor modify EditText
. Just put your code in view.post
Kotlin
mEditText.post {
mEditText.setText("your text")
}
Java
mEditText.post(newRunnable() {
@Overridevoidrun() {
mEditText.setText("your text");
}
});
Solution 2:
I found a way to avoid the problem: consuming the touch events I'm interested in.
I want to react to clicks on certain corner of the edit box (think of a clear text button, or a view password one).
I first tried to use only onTouchEvent because I needed the coordinates. But the IDE warned me about not overriding performClick too for accessibility reasons. The idea is if you add extra behavior on touch, it should be available also to accessibility tools that call performClick
.
But it's not the case. The extra buttons are extra functionality that doesn't need to be available. Or, if it is available, it won't be available as a normal/general click on the edit box. Maybe as an accessibility action implemented somewhere else.
So, conclusion: I can use only onTouchEvent and consume the events.
Solution
overridefunonTouchEvent(event: MotionEvent?): Boolean {
event!!
ensureDrawingInfo()
val touchArea = pluginAreaFromMotionEvent(event)
if (touchArea != null) {
// it's the special place, I will handle thiswhen (event.actionMasked) {
MotionEvent.ACTION_DOWN -> lastTouchArea = touchArea
MotionEvent.ACTION_CANCEL -> lastTouchArea = null
MotionEvent.ACTION_UP -> {
if (lastTouchArea == touchArea) { // proper click on place// call the plugin
plugins[touchArea.pluginIndex].onClick(this, it.rightSide, ::invalidateDrawingInfo)
}
}
// consume touch events that happen on MY areareturntrue
}
returnsuper.onTouchEvent(event)
}
Why
The EditText editor opens sometimes a suggestion window in response to touch events. And it wants to replace text in certain position. But it doesn't replace it right away. It enqueues a message to be processed later, using a looper) so when the replace takes place the text might have been changed by me. I even tried to enqueue my clearing code in the hope that it happens later, but I was unlucky.
The cleanest solution is to consume the events relative to the areas I'm handling so the editor doesn't provoke any problems.
In fact, running this on an API 27 emulator I couldn't reproduce the error. I guess the editor code changed to be aware that the text could have been changed in between.
Post a Comment for "Edittext Crashing On Clearing Text"