简体   繁体   中英

Shows ClassCastException while performing drag and drop in android

I'm performing simple drag and drop in android. Log message shows ClassCastException and some error in line number 70 of the java code. Line number 70 is inside onDrag(View layoutview, DragEvent dragEvent) method and in case DragEvent.ACTION_DROP. That line is within ** **. I don't know what I'm doing wrong. Thanks in advance.

THIS IS THE JAVA CODE

package com.example.anandzoom.draganddrop;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

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

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

    findViewById(R.id.text_drag).setOnTouchListener(this);
    findViewById(R.id.text_drop).setOnDragListener(this);

}

public boolean onTouch(View view, MotionEvent motionEvent)
{
    if(motionEvent.getAction() == MotionEvent.ACTION_DOWN)
    {
        View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
        view.startDrag(null, shadowBuilder, view, 0);
        view.setVisibility(View.INVISIBLE);
        return true;
    }
    else
    {
        return false;
    }
}


public boolean onDrag(View layoutview, DragEvent dragEvent)
{
    int action = dragEvent.getAction();
    switch (action)
    {
        case DragEvent.ACTION_DRAG_STARTED:
        {
            Log.v("DragAndDrop", "ACTION_DRAG_STARTED");
            break;
        }
        case DragEvent.ACTION_DRAG_ENTERED:
        {
            Log.v("DragAndDrop", "ACTION_DRAG_ENTERED");
            break;
        }
        case DragEvent.ACTION_DRAG_EXITED:
        {
            Log.v("DragAndDrop", "ACTION_DRAG_EXITED");
            break;
        }
        case DragEvent.ACTION_DROP:
        {
            Log.v("DragAndDrop", "ACTION_DROP");

            TextView view = (TextView) dragEvent.getLocalState();
            ViewGroup owner = (ViewGroup) layoutview.getParent();
            owner.removeView(view);
          **LinearLayout container = (LinearLayout) layoutview;**
            container.addView(view);
            view.setVisibility(View.VISIBLE);
            break;
        }
        case DragEvent.ACTION_DRAG_ENDED:
        {
            Log.v("DragAndDrop", "ACTION_DRAG_ENDED");
            break;
        }
        default:
        {
            break;
        }
    }
    return true;
}

}

THIS IS THE XML CODE

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.anandzoom.draganddrop.MainActivity">

<LinearLayout
    android:id="@+id/layout_top"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="16dp">

    <TextView
        android:id="@+id/text_drag"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Drag me"
        android:textSize="19sp"/>

</LinearLayout>

<LinearLayout
    android:id="@+id/layout_bottom"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="16dp">

    <TextView
        android:id="@+id/text_drop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Drop here"
        />
</LinearLayout>

THE LOG MESSAGE SHOWS THE ERROR: ClassCastException

D/AndroidRuntime: Shutting down VM
10-13 14:58:35.635 1270-1270/com.example.anandzoom.draganddrop E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             Process: com.example.anandzoom.draganddrop, PID: 1270
                                                                             Theme: themes:{default=overlay:com.resurrectionremix.pitchblack, iconPack:org.cyanogenmod.hexolibre, fontPkg:com.resurrectionremix.pitchblack, com.android.systemui=overlay:com.resurrectionremix.pitchblack, com.android.systemui.navbar=overlay:com.resurrectionremix.pitchblack}
                                                                             java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.LinearLayout
                                                                                 at com.example.anandzoom.draganddrop.MainActivity.onDrag(MainActivity.java:70)
                                                                                 at android.view.View.dispatchDragEvent(View.java:19702)
                                                                                 at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1518)
                                                                                 at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1518)
                                                                                 at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1518)
                                                                                 at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1518)
                                                                                 at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1518)
                                                                                 at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1518)
                                                                                 at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1518)
                                                                                 at android.view.ViewRootImpl.handleDragEvent(ViewRootImpl.java:5290)
                                                                                 at android.view.ViewRootImpl.-wrap6(ViewRootImpl.java)
                                                                                 at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3465)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                 at android.os.Looper.loop(Looper.java:148)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5461)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

您已为文本视图设置拖动侦听器,并且您尝试将文本视图强制转换为方法中的线性布局。

Your TextView in XML is as below, this is where you are setting the Drag Listener.

<TextView
        android:id="@+id/text_drop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Drop here"
        />

Now you are typecasting a TextView on to a LinearLayout , which has nothing to do with your Drag and Drop code hence the exception.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM