1

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!

LoneLegend
  • 18
  • 13
  • `when i try to get the tag of the current view` Does the view have a tag? It says you're calling `null.toString()`. The only place you could be calling that is `v.getTag().toString()`, the line 40, which means the tag is `null`. You'll need to [`setTag`](https://developer.android.com/reference/android/view/View#setTag(java.lang.Object)) first. – Eugen Pechanec Jul 21 '20 at 09:18
  • @EugenPechanec Check the updated post. – LoneLegend Jul 21 '20 at 10:30
  • C'mon, it's the same type of error, at least try and make a guess, what you think this means. Does it have a tag? No. Boom, `NullPointerException` if you try to work with it. Does it have a __________? No. Same thing happens. – Eugen Pechanec Jul 21 '20 at 10:47
  • it does, I added a tag. – LoneLegend Jul 21 '20 at 10:51
  • Yes, but you're not trying to *set color filter* on the *tag*, right? Read the error messages. – Eugen Pechanec Jul 21 '20 at 10:55
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/218273/discussion-between-lonelegend-and-eugen-pechanec). – LoneLegend Jul 21 '20 at 10:58
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Eugen Pechanec Jul 26 '20 at 08:09

0 Answers0