双击以从 editText 中选择文本导致 ArithmeticException

Posted

技术标签:

【中文标题】双击以从 editText 中选择文本导致 ArithmeticException【英文标题】:double click to select text from editText cause an ArithmeticException 【发布时间】:2016-02-26 19:31:32 【问题描述】:

在某些设备中,当我尝试从 edittext 中选择文本进行复制时,会导致

算术异常

我在活动中使用该编辑文本扩展自 AppCompatActivity

这里是例外

E/MessageQueue-JNI:MessageQueue 回调中的异常: handleReceiveCallback E/MessageQueue-JNI: java.lang.ArithmeticException:在除以零 android.widget.Editor.updateShowAsAction (Editor.java:6034) android.widget.Editor$SelectionActionModeCallback.onCreateActionMode (编辑器.java:3208) com.android.internal.policy.impl.PhoneWindow$DecorView$ActionModeCallbackWrapper.onCreateActionMode (PhoneWindow.java:3522) android.support.v7.internal.view.SupportActionModeWrapper$CallbackWrapper.onCreateActionMode (SupportActionModeWrapper.java:155) android.support.v7.app.AppCompatDelegateImplV7$ActionModeCallbackWrapperV7.onCreateActionMode (AppCompatDelegateImplV7.java:1644) android.support.v7.app.AppCompatDelegateImplV7.startSupportActionModeFromWindow (AppCompatDelegateImplV7.java:748) android.support.v7.app.AppCompatDelegateImplV7.startSupportActionMode (AppCompatDelegateImplV7.java:642) android.support.v7.app.AppCompatDelegateImplV14$AppCompatWindowCallbackV14.startAsSupportActionMode (AppCompatDelegateImplV14.java:74) android.support.v7.app.AppCompatDelegateImplV14$AppCompatWindowCallbackV14.onWindowStartingActionMode (AppCompatDelegateImplV14.java:58) com.android.internal.policy.impl.PhoneWindow$DecorView.startActionMode (PhoneWindow.java:2981) com.android.internal.policy.impl.PhoneWindow$DecorView.startActionModeForChild (PhoneWindow.java:2968) android.view.ViewGroup.startActionModeForChild (ViewGroup.java:710) android.view.ViewGroup.startActionModeForChild (ViewGroup.java:710) android.view.ViewGroup.startActionModeForChild (ViewGroup.java:710) android.view.ViewGroup.startActionModeForChild (ViewGroup.java:710) android.view.ViewGroup.startActionModeForChild (ViewGroup.java:710) android.view.ViewGroup.startActionModeForChild (ViewGroup.java:710) android.view.ViewGroup.startActionModeForChild (ViewGroup.java:710) android.view.ViewGroup.startActionModeForChild (ViewGroup.java:710) android.view.ViewGroup.startActionModeForChild (ViewGroup.java:710) android.view.ViewGroup.startActionModeForChild (ViewGroup.java:710) android.view.ViewGroup.startActionModeForChild (ViewGroup.java:710) android.view.ViewGroup.startActionModeForChild (ViewGroup.java:710) android.view.ViewGroup.startActionModeForChild (ViewGroup.java:710) android.view.View.startActionMode (View.java:5292) android.widget.Editor.startSelectionActionMode (编辑器.java:1723) android.widget.Editor$SelectionModifierCursorController.onTouchEvent (Editor.java:5500) android.widget.Editor.onTouchEvent (Editor.java:1211) android.widget.TextView.onTouchEvent (TextView.java:9567) android.view.View.dispatchTouchEvent (View.java:8975) android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2709) android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2371) android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2709) android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2371) android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2709) android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2371) android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2709) android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2371) android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2709) android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2371) android.widget.ScrollView.dispatchTouchEvent (滚动视图.java:615) android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2709) android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2371) android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2709) android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2371) android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2709) android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2371) android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2709) android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2371) android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2709) android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2371) android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2709) android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2371) android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2709) android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2371) android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2709) android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2371) android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2709) android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2371) com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent (PhoneWindow.java:2568) com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent (PhoneWindow.java:1776) android.app.Activity.dispatchTouchEvent (活动.java:2866) android.support.v7.internal.view.WindowCallbackWrapper.dispatchTouchEvent (WindowCallbackWrapper.java:60) com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent (PhoneWindow.java:2529) android.view.View.dispatchPointerEvent (查看.java:9180) android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent (ViewRootImpl.java:4734) android.view.ViewRootImpl$ViewPostImeInputStage.onProcess (ViewRootImpl.java:4572) android.view.ViewRootImpl$InputStage.deliver (ViewRootImpl.java:4096) android.view.ViewRootImpl$InputStage.onDeliverToNext (ViewRootImpl.java:4149) android.view.ViewRootImpl$InputStage.forward (ViewRootImpl.java:4115) android.view.ViewRootImpl$AsyncInputStage.forward (ViewRootImpl.java:4229) android.view.ViewRootImpl$InputStage.apply (ViewRootImpl.java:4123) android.view.ViewRootImpl$AsyncInputStage.apply (ViewRootImpl.java:4286) android.view.ViewRootImpl$InputStage.deliver (ViewRootImpl.java:4096) android.view.ViewRootImpl$InputStage.onDeliverToNext (ViewRootImpl.java:4149) android.view.ViewRootImpl$InputStage.forward (ViewRootImpl.java:4115) android.view.ViewRootImpl$InputStage.apply (ViewRootImpl.java:4123) android.view.ViewRootImpl$InputStage.deliver (ViewRootImpl.java:4096) android.view.ViewRootImpl.deliverInputEvent (ViewRootImpl.java:6594) android.view.ViewRootImpl.doProcessInputEvents (ViewRootImpl.java:6484) android.view.ViewRootImpl.enqueueInputEvent (ViewRootImpl.java:6455) android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent (ViewRootImpl.java:6684) android.view.InputEventReceiver.dispatchInputEvent (InputEventReceiver.java:185) android.os.MessageQueue.nativePollOnce (MessageQueue.java) android.os.MessageQueue.next (MessageQueue.java:143) android.os.Looper.loop (Looper.java:130) android.app.ActivityThread.main (ActivityThread.java:5944) java.lang.reflect.Method.invoke (Method.java) java.lang.reflect.Method.invoke (Method.java:372) com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1399)

此方法中的异常原因

android.widget.Editor.updateShowAsAction (Editor.java:6034)

这是我编辑文本的 xml 代码

            <EditText 
            android:id="@+id/etIdNumber"
            android:hint="@string/user_profile_myInfo_add_identification_dialog_et_hint"
            android:inputType="number"
            style="@style/et_rounded_corner_white_bg"
            />

这是我的风格

    <style name="et_rounded_corner_white_bg" parent="horizontal_margin">
     <item name="android:layout_width">match_parent</item>
     <item name="android:layout_height">wrap_content</item>
     <item name="android:textColor">@color/black</item>
     <item name="android:background">@drawable/editfield_bg</item>
     <item name="android:textColorHint">@color/font_very_light_gray</item>
     <item name="android:padding">@dimen/padding_5dp</item>
     <item name="android:layout_marginBottom">@dimen/padding_10dp</item>
     <item name="android:textCursorDrawable">@null</item>
</style>

【问题讨论】:

贴出相关代码 @johnrao07 没有具体的代码,我把它变成了一个普通的编辑文本 在哪些设备上导致问题? @johnrao07 三星 Galaxy Note4 和三星 Grand 其他设备也能正常工作?这些设备是什么? 【参考方案1】:

当您双击 EditText 后设备在操作栏中设置复制/粘贴操作按钮时,会发生此错误。它将检索您的 actionButtonStyle 的 minWidth 属性。不要将此设置为 0dp,而是使用 1dp。代码示例:

<item name="android:actionButtonStyle">@style/myTheme.ActionButtonStyle</item>

<style name="myTheme.ActionButtonStyle" parent="@android:style/Widget.Holo.Light.ActionButton">
    <item name="android:background">@drawable/actionbutton_bg</item>
    <item name="android:textColor">@color/title_foreground</item>
    <item name="android:textSize">16sp</item>
    <item name="android:textStyle">normal</item>
    <item name="android:minWidth">1dp</item>
    <item name="android:paddingLeft">12dp</item>
    <item name="android:paddingStart">12dp</item>
    <item name="android:paddingRight">12dp</item>
    <item name="android:paddingEnd">12dp</item>
</style>

【讨论】:

【参考方案2】:

它会做的工作!

<EditText 
 android:id="@+id/etIdNumber"
 android:hint="@string/user_profile_myInfo_add_identification_dialog_et_hint"
 android:inputType="number"
 style="@style/et_rounded_corner_white_bg"/>

从样式中移除这一行:

<item name="android:textCursorDrawable">@null</item>

【讨论】:

当我添加此行时它不起作用,键盘没有打开

以上是关于双击以从 editText 中选择文本导致 ArithmeticException的主要内容,如果未能解决你的问题,请参考以下文章

如何确定在 EditText 中选择了哪些文本? [复制]

Android - 防止edittext复制/粘贴并双击全选[重复]

在多个膨胀的 EditTexts 上设置文本会导致旋转后全部填充相同的文本

UICollectionView didselect 和双击冲突

UITextView 双击编辑

自定义ANDROID中EDITTEXT中的HINT文本的大小