在android中执行拖放操作时显示ClassCastException

时间:2016-10-13 09:25:14

标签: java android xml classcastexception

我在android中执行简单的拖放操作。日志消息显示ClassCastException和java代码的第70行中的一些错误。 行号70位于onDrag(View layoutview,DragEvent dragEvent)方法内,以及DragEvent.ACTION_DROP。那条线在** **之内。我不知道我做错了什么。提前谢谢。

这是JAVA代码

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;
}

}

这是XML代码

<?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>

日志消息显示错误: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)

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

您在Text中的TextView如下所示,这是您设置Drag Listener的地方。

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

现在你将TextView类型转换为LinearLayout,这与你的拖放代码无关,因此是例外。