我正在关注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并将其移动到放置目标时,它将触发诸如更改背景颜色的操作。但是我仍然坚持这个问题。请帮忙。我真是个菜鸟。
最佳答案
使用startDrag()
而不是startDragAndDrop()
。与startDragAndDrop()
不同,它被标记为已弃用,但可以工作。它可能与API版本有关。