没有虚方法startDragAndDrop

时间:2016-09-30 14:28:41

标签: java android drag-and-drop

我正在关注https://www.youtube.com/watch?v=L-bndS0Z4r8教程但收到此错误。请帮忙。

MainActivity.java

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



    findViewById(R.id.textView).setOnLongClickListener(longListen);
    findViewById(R.id.textView2).setOnLongClickListener(longListen);
    findViewById(R.id.textView3).setOnLongClickListener(longListen);
}
View.OnLongClickListener longListen = new View.OnLongClickListener() {
    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public boolean onLongClick(View v) {
        DragShadow dragShadow = new DragShadow(v);

        ClipData data = ClipData.newPlainText("",  "");
            v.startDragAndDrop(data,dragShadow,v,0);

        return false;
    }
};

private class DragShadow extends View.DragShadowBuilder{

    ColorDrawable grayBox;

    DragShadow(View view) {
        super(view);
        grayBox = new ColorDrawable(Color.LTGRAY);
    }

    @Override
    public void onProvideShadowMetrics(Point outShadowSize, Point outShadowTouchPoint) {
        super.onProvideShadowMetrics(outShadowSize,outShadowTouchPoint);
       View v =getView();

        int h = v.getHeight()/2;
        int w = v.getWidth()/2;

        grayBox.setBounds(0,0,w,h);

        outShadowSize.set(w,h);

        outShadowTouchPoint.set(w/2,h/2);
    }

    @Override
    public void onDrawShadow(Canvas canvas) {
        super.onDrawShadow(canvas);
       grayBox.draw(canvas);

    }
}

Main_Activity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:orientation="vertical"
android:baselineAligned="false">


<TextView
    android:text="Test 1"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:id="@+id/textView"
    android:layout_gravity="center"
    android:layout_weight="0.25" />

<TextView
    android:text="Test2"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:id="@+id/textView2"
    android:layout_gravity="center"
    android:layout_weight="0.25" />

<TextView
    android:text="Test3"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:id="@+id/textView3"
    android:layout_gravity="center"
    android:layout_weight="0.25" />

<TextView
    android:text="Drop target"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:id="@+id/textView4"
    android:layout_gravity="center"
    android:layout_weight="0.25" />

错误

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.moon.draganddrop, PID: 3077
              java.lang.NoSuchMethodError: No virtual method startDragAndDrop(Landroid/content/ClipData;Landroid/view/View$DragShadowBuilder;Ljava/lang/Object;I)Z in class Landroid/view/View; or its super classes (declaration of 'android.view.View' appears in /system/framework/framework.jar:classes2.dex)
                  at com.example.moon.draganddrop.MainActivity$1.onLongClick(MainActivity.java:41)
                  at android.view.View.performLongClick(View.java:5237)
                  at android.widget.TextView.performLongClick(TextView.java:9228)
                  at android.view.View$CheckForLongPress.run(View.java:21121)
                  at android.os.Handler.handleCallback(Handler.java:739)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:148)
                  at android.app.ActivityThread.main(ActivityThread.java:5417)
                  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)

基本上我想创建一个拖放应用程序。过程是在单击textview并将其移动到放置目标时,它将触发诸如更改背景颜色之类的内容。但我仍然坚持这个问题。请帮忙。我是个菜鸟。

2 个答案:

答案 0 :(得分:1)

使用startDrag()代替startDragAndDrop()。它被标记为已弃用,但与startDragAndDrop()不同,它可以正常工作。它可能与API版本有关。

答案 1 :(得分:1)

问题在于您尝试利用的设备API的当前版本。 StartDragAndDrop方法仅适用于API版本24或Nougat(android 7.0)。要解决你应该添加:

if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) {
   v.startDragAndDrop(data,dragShadow,v,0);
} else {
   v.startDrag(data,dragShadow,v,0);
}