Android: drag and drop error when drooping the view that is being draged inside EditText

Solution for Android: drag and drop error when drooping the view that is being draged inside EditText
is Given Below:

I want to create a Layers View that holds different Layers and Folder (such as those used in Photoshop). I first tried using a Recyclerview with Adapter and it worked pretty well until I started working on the Drag and Drop capabilities of the recyclerview.

I searched for adding drag and drop to a recyclerview and I saw this post with explanation using ItemTouchHelper. I have successfully implemented it and it works but only if all elements are in the same depth, in other words when there were no folder. But I wanted to add folder so I had to add visuals that indicate where the layer should be placed either on top or bottom of another layer or inside a folder.

So instead of using Recyclerview I implemented my own view that extends LinearLayout, and uses Vertical orientation to add children to the layout by manually inflating the xml file. And everything works well since it does not contain large amount of inner View there is no real difference in term of performance when compared with Recyclerview . Then I implemented the Drag and Drop functionality by overriding the onDrag function and using the DragEvent.

enter image description here

The problem comes when I drop the view that is being dragged inside the EditText used in the layout xml file. I got the following error

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.slaviboy.lowpolyartpro, PID: 9193
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.ClipData.getItemCount()' on a null object reference
    at android.widget.Editor.onDrop(Editor.java:2814)
    at android.widget.TextView.onDragEvent(TextView.java:12922)
    at androidx.appcompat.widget.AppCompatEditText.onDragEvent(AppCompatEditText.java:281)
    at android.view.View.callDragEventHandler(View.java:26595)
    at android.view.View.dispatchDragEvent(View.java:26583)
    at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1819)
    at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1819)
    at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1819)
    at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1819)
    at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1819)
    at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1819)
    at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1819)
    at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1819)
    at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1819)
    at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1819)
    at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1819)
    at android.view.ViewRootImpl.handleDragEvent(ViewRootImpl.java:7258)
    at android.view.ViewRootImpl.access$1200(ViewRootImpl.java:191)
    at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:5098)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7656)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

I manage to fix the problem by extending the EditText class and overriding the onDragEvent function, then instead of calling the super class I just return true.

open class EditText : AppCompatEditText {

    constructor(context: Context) : super(context) {
        setAttributes(context, null)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        setAttributes(context, attrs)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        setAttributes(context, attrs)
    }
  
    private fun setAttributes(context: Context, attrs: AttributeSet?) {

        // get xml attributes
        val typedArray: TypedArray = context.obtainStyledAttributes(attrs, R.styleable.EditText)

        ...

        typedArray.recycle()
    }

    override fun onDragEvent(event: DragEvent?): Boolean {
        return true  // return super.onDragEvent(event)
    } 
}

So I have two questions:

  1. What causes the error?
  2. Is there a better way of solving it?