EditText 在清除文本时崩溃 [英] EditText crashing on clearing text

查看:53
本文介绍了EditText 在清除文本时崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想清除 EditText 中的文本.我试过了:

I want to clear the text from an EditText. I tried:

  • editText.setText("")
  • editText.text.clear()
  • editText.editableText.clear()

它有效.但有时会有例外.

and it works.But sometimes there’s an exception.

2018-12-11 16:23:44.367 13388-13388/com.example.helios.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.helios.myapplication, PID: 13388
    java.lang.IndexOutOfBoundsException: setSpan (0 ... 6) ends beyond length 0
        at android.text.SpannableStringBuilder.checkRange(SpannableStringBuilder.java:1265)
        at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:684)
        at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:677)
        at android.widget.Editor$SuggestionsPopupWindow.updateSuggestions(Editor.java:3608)
        at android.widget.Editor$SuggestionsPopupWindow.show(Editor.java:3480)
        at android.widget.Editor.replace(Editor.java:359)
        at android.widget.Editor$3.run(Editor.java:2129)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6119)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776

我从重写的 performClick() 执行该代码(我正在创建 EditText 的子类).

I execute that code from an overridden performClick() (I'm creating a subclass of EditText).

EditText 似乎没有意识到它应该取消/忘记建议机制并且失败了.

It seems that EditText doesn’t realize it should cancel/forget the suggestion mechanism and it fails.

你知道我应该做些什么来取消/防止建议失败吗?

Do you know if there's anything special I should do to cancel/prevent suggestions to fail?

我注意到如果我让弹出窗口出现,然后在 X 处单击两次(第一个关闭弹出窗口,第二个执行清除代码)会发生异常.

I noticed that the exception happens if I make the popup window to appear, then click twice at X (first one dismiss the popup, second one executes the clearing code).

更新:

代码的相关部分.实际代码有一个插件系统来添加不同的绘图和动作,但相关代码是这样的:

Relevant parts of the code. The actual code has a plugin system to add different drawings and actions, but the relevant code is this one:

    class MyEditText(..): EditText(context) {
    ... 
        override fun onTouchEvent(event: MotionEvent?): Boolean {
            event!!
            ensureDrawingInfo()
            when (event.actionMasked) {
                MotionEvent.ACTION_DOWN, MotionEvent.ACTION_CANCEL, MotionEvent.ACTION_UP -> {
                    lastTouchArea = pluginAreaFromMotionEvent(event)
                }
            }
            return super.onTouchEvent(event)
        }

        override fun performClick(): Boolean {
            lastTouchArea?.let {
                plugins[it.pluginIndex].onClick(this, it.rightSide, ::invalidateDrawingInfo)
                return true
            }
            return super.performClick()
        }


    class MyPlugin(val drawableLeft: Drawable, val drawableRight: Drawable) : Plugin {
        ....

        override fun onClick(editText: MyEditText, rightSide: Boolean, redraw: () -> Unit) {
            if (rightSide) {
                // X
                editText.text.clearSpans()
                editText.text.clear()
            }
        }
    }

推荐答案

问题是updateSuggestions"在您更改 EditText 中的文本后尝试 setSpan.所以你需要在编辑器修改 EditText 后进行文本更改.只需将您的代码放在 view.post

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

科特林

mEditText.post {
   mEditText.setText("your text")
}

Java

mEditText.post(new Runnable() {
        @Override
        void run() {
            mEditText.setText("your text");
        }
    });

这篇关于EditText 在清除文本时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆