TransWikia.com

Why is my App crashing when I drag an element?

Stack Overflow Asked by LoneLegend on November 4, 2021

I am trying to make an app in which you can drag and drop specific elements and create your own UI. However when I hold that element and try to drag it, the app crashes. The error is occuring at line 59: v.getBackground().setColorFilter(Color.GRAY, PorterDuff.Mode.SRC_IN);, where I am trying to set a color filter to the background of the current view that is being dragged.

Code:

package com.app.dragndrop;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ClipData;
import android.content.ClipDescription;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.util.Log;
import android.view.DragEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.app.dragndrop.R;

public class MainActivity extends AppCompatActivity implements View.OnDragListener, View.OnLongClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView myText = findViewById(R.id.myText);

        myText.setOnLongClickListener(this);
        myText.setOnDragListener(this);
    }

    @Override
    public boolean onLongClick(View v) {
        ClipData.Item item = new ClipData.Item((CharSequence) v.getTag());
        String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
        ClipData data = new ClipData(v.getTag().toString(), mimeTypes, item);
        View.DragShadowBuilder dragshadow = new View.DragShadowBuilder(v);
        v.startDrag(data
                , dragshadow
                , v
                , 0
        );
        return true;
    }

    @Override
    public boolean onDrag(View v, DragEvent event) {
        int action = event.getAction();
        switch (action) {

            case DragEvent.ACTION_DRAG_STARTED:
                if (event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
                    return true;
                }
                return false;

            case DragEvent.ACTION_DRAG_ENTERED:
                v.getBackground().setColorFilter(Color.GRAY, PorterDuff.Mode.SRC_IN);
                v.invalidate();
                return true;

            case DragEvent.ACTION_DRAG_LOCATION:
                return true;

            case DragEvent.ACTION_DRAG_EXITED:
                v.getBackground().clearColorFilter();
                v.invalidate();
                return true;

            case DragEvent.ACTION_DROP:
                ClipData.Item item = event.getClipData().getItemAt(0);
                String dragData = item.getText().toString();
                Toast.makeText(this, "Dragged data is " + dragData, Toast.LENGTH_SHORT).show();
                v.getBackground().clearColorFilter();
                v.invalidate();

                View vw = (View) event.getLocalState();
                ViewGroup owner = (ViewGroup) vw.getParent();
                owner.removeView(vw);
                LinearLayout container = (LinearLayout) v;
                container.addView(vw);
                vw.setVisibility(View.VISIBLE);
                return true;

            case DragEvent.ACTION_DRAG_ENDED:
                v.getBackground().clearColorFilter();
                v.invalidate();
                if (event.getResult())
                    Toast.makeText(this, "The drop was handled.", Toast.LENGTH_SHORT).show();
                else
                    Toast.makeText(this, "The drop didn't work.", Toast.LENGTH_SHORT).show();
                return true;
            default:
                Log.e("DragDrop Example", "Unknown action type received by OnDragListener.");
                break;
        }
        return false;
    }
}

Error:

2020-07-21 15:08:15.596 4515-4515/com.app.dragndrop E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.app.dragndrop, PID: 4515
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.graphics.drawable.Drawable.setColorFilter(int, android.graphics.PorterDuff$Mode)' on a null object reference
        at com.app.dragndrop.MainActivity.onDrag(MainActivity.java:59)
        at android.view.View.callDragEventHandler(View.java:24446)
        at android.view.ViewRootImpl.setDragFocus(ViewRootImpl.java:6678)
        at android.view.View.dispatchDragEvent(View.java:24435)
        at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1759)
        at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1759)
        at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1759)
        at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1759)
        at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1759)
        at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1759)
        at android.view.ViewRootImpl.handleDragEvent(ViewRootImpl.java:6557)
        at android.view.ViewRootImpl.access$1100(ViewRootImpl.java:141)
        at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:4422)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:235)
        at android.app.ActivityThread.main(ActivityThread.java:6760)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:911)

Thanks for the help!

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP