在android中触摸EditText时应用程序崩溃

Posted

技术标签:

【中文标题】在android中触摸EditText时应用程序崩溃【英文标题】:Application crashes when EditText is touched in android 【发布时间】:2021-06-12 01:08:08 【问题描述】:

EditText 视图为空时,我可以触摸它并开始输入数据。但是当我在输入一些数据时触摸它时,应用程序崩溃,并给出以下堆栈跟踪:

    E/InputEventReceiver: Exception dispatching input event.
    D/androidRuntime: Shutting down VM
    E/AndroidRuntime: FATAL EXCEPTION: main
        Process: com.example.bullscows, PID: 31754
        android.content.res.Resources$NotFoundException: Resource ID #0x0
            at android.content.res.ResourcesImpl.getValueForDensity(ResourcesImpl.java:293)
            at android.content.res.Resources.getDrawableForDensity(Resources.java:1022)
            at android.content.res.Resources.getDrawable(Resources.java:965)
            at android.content.Context.getDrawable(Context.java:605)
            at android.widget.Editor$InsertionPointCursorController.getHandle(Editor.java:5219)
            at android.widget.Editor$InsertionPointCursorController.show(Editor.java:5198)
            at android.widget.Editor.onTouchUpEvent(Editor.java:2247)
            at android.widget.TextView.onTouchEvent(TextView.java:9809)
            at android.view.View.dispatchTouchEvent(View.java:11891)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3024)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2680)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3024)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2680)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3024)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2680)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3024)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2680)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3024)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2680)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3024)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2680)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3024)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2680)
            at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:449)
            at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1837)
            at android.app.Activity.dispatchTouchEvent(Activity.java:3405)
            at androidx.appcompat.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69)
            at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:411)
            at android.view.View.dispatchPointerEvent(View.java:12135)
            at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:5158)
            at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4968)
            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4494)
            at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4547)
            at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4513)
            at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4640)
            at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4521)
            at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4697)
            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4494)
            at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4547)
            at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4513)
            at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4521)
            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4494)
            at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:7096)
            at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:7070)
            at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:7027)
            at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:7226)
            at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:193)
            at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
            at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:182)
            at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:7181)
            at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:7250)
    E/AndroidRuntime:     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:999)
            at android.view.Choreographer.doCallbacks(Choreographer.java:811)
            at android.view.Choreographer.doFrame(Choreographer.java:733)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:985)
            at android.os.Handler.handleCallback(Handler.java:795)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:166)
            at android.app.ActivityThread.main(ActivityThread.java:6861)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:450)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)

我不确定我应该提供哪些代码,因为我有几个类和已经很复杂的逻辑,所以我认为我应该首先了解问题的真正原因。

这是EditText的xml代码:

<EditText
    android:id="@+id/input"
    android:importantForAutofill="no"
    android:textSelectHandle="@null"
    android:visibility="visible"
    android:layout_
    android:layout_
    android:layout_centerHorizontal="true"
    android:layout_alignParentTop="true"
    android:background="@drawable/rounded_field"
    android:inputType="number"
    android:textCursorDrawable="@null"
    android:padding="8dp"
    android:layout_marginTop="12dp"
    android:layout_marginBottom="16dp"
    android:textSize="22sp"
    tools:ignore="LabelFor" />

【问题讨论】:

【参考方案1】:

不需要android:textSelectHandle="@null" 的原因是当您单击可编辑文本中的某个点时,会出现一个指针。将其设置为@null 将阻止它出现,因为没有与该参数对应的此类资源,这会导致程序崩溃。 将其设置为"@android:color/transparent"

【讨论】:

据我所知,android:textCursorDrawable="@null" 可以吗?光标在设置时似乎采用默认值。 这很好,因为 android:textCursorDrawable 值是对将在插入光标绘制的可绘制对象的引用。如果它不存在,这是可以接受的。另一方面,android:textSelectHandle 值是对可用于显示文本选择锚以在文本中定位光标的可绘制对象的引用。如果什么都没有,那就有问题了。此外,并非所有属性都接受所有基本类型的参数。【参考方案2】:

我明白了。当我更改了这些行时:

android:textSelectHandle="@null"

android:textCursorDrawable="@null"

一切恢复正常。我应该使用透明预设或其他东西而不是@nulls。

【讨论】:

以上是关于在android中触摸EditText时应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章

Android键盘在Page Renderer中失去了对触摸的关注

触摸时 EditText 未突出显示

Android移动应用中的焦点分析

单击或触摸另一个视图时清除EditText焦点并隐藏键盘

EditText表现得很奇怪

android中的虚拟键盘问题