简体   繁体   English

防止在TextView中执行剪切操作时应用崩溃

[英]Prevent app crash when cut operation in TextView

I put text-selectable TextView in Android layout with setTextIsSelectable(true) and the text is set by Html.fromHtml(sometexts) . 我使用setTextIsSelectable(true)将可文本选择的TextView放在Android布局中,并且文本由Html.fromHtml(sometexts)
It works good to allow user to copy text. 允许用户复制文本效果很好。 My JellyBean device gives small baloons with cut/copy/paste buttons on top of the selected text. 我的JellyBean设备在选定文本的顶部提供了带有剪切/复制/粘贴按钮的小气球。

But when user accidently click cut icon left side of the copy menu, it crashes. 但是,当用户不小心单击复制菜单左侧的剪切图标时,它将崩溃。

The log is given as below but there is no point to handle this exception in my application. 日志如下所示,但是在我的应用程序中没有必要处理此异常。
How to prevent app crash? 如何防止应用程式当机?

01-06 19:29:16.025    2048-2048/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.ClassCastException: android.text.SpannableString cannot be cast to android.text.Editable
        at android.widget.TextView.deleteText_internal(TextView.java:8865)
        at android.widget.TextView.onTextContextMenuItem(TextView.java:8315)
        at android.widget.Editor$ActionPopupWindow.onClick(Editor.java:3556)
        at android.view.View.performClick(View.java:4114)
        at android.view.View$PerformClick.run(View.java:17097)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4885)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
        at dalvik.system.NativeStart.main(Native Method)

I'm not sure and I can't test it right now but maybe try to put that in your TextView xml : 我不确定,现在无法测试,但也许尝试将其放在您的TextView xml中:

android:bufferType="spannable"

If it's still not working you can implement yourself your action for the selectable event. 如果仍然无法正常工作,则可以对可选事件实施自己的操作。 Look at the answer of this question here : Android- How can I show text selection on textview? 在这里查看此问题的答案: Android-如何在textview上显示文本选择?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM