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

Application crashes when EditText is touched in android

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)

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

这是 xml EditText 的代码:

<EditText
    android:id="@+id/input"
    android:importantForAutofill="no"
    android:textSelectHandle="@null"
    android:visibility="visible"
    android:layout_width="180dp"
    android:layout_height="wrap_content"
    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" />

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

android:textSelectHandle="@null"

android:textCursorDrawable="@null"

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

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