I am trying to use inbuilt drag and drop functionality in android 4+. I have created my custom TextView for this purpose as
public class DragSpotTextView extends TextView
The textview drag is working fine but when I drag the view on some dragspot(this is also textview) its giving exception as;
01-17 16:16:29.178: E/AndroidRuntime(1193): FATAL EXCEPTION: main
01-17 16:16:29.178: E/AndroidRuntime(1193): java.lang.ClassCastException: java.lang.String cannot be cast to android.text.Spannable
01-17 16:16:29.178: E/AndroidRuntime(1193): at android.widget.TextView.onDragEvent(TextView.java:11223)
01-17 16:16:29.178: E/AndroidRuntime(1193): at android.view.View.dispatchDragEvent(View.java:13465)
01-17 16:16:29.178: E/AndroidRuntime(1193): at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1104)
01-17 16:16:29.178: E/AndroidRuntime(1193): at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1104)
01-17 16:16:29.178: E/AndroidRuntime(1193): at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1104)
01-17 16:16:29.178: E/AndroidRuntime(1193): at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1104)
01-17 16:16:29.178: E/AndroidRuntime(1193): at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1104)
01-17 16:16:29.178: E/AndroidRuntime(1193): at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1104)
01-17 16:16:29.178: E/AndroidRuntime(1193): at android.view.ViewRootImpl.handleDragEvent(ViewRootImpl.java:3471)
01-17 16:16:29.178: E/AndroidRuntime(1193): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2620)
01-17 16:16:29.178: E/AndroidRuntime(1193): at android.os.Handler.dispatchMessage(Handler.java:99)
01-17 16:16:29.178: E/AndroidRuntime(1193): at android.os.Looper.loop(Looper.java:137)
01-17 16:16:29.178: E/AndroidRuntime(1193): at android.app.ActivityThread.main(ActivityThread.java:4424)
01-17 16:16:29.178: E/AndroidRuntime(1193): at java.lang.reflect.Method.invokeNative(Native Method)
01-17 16:16:29.178: E/AndroidRuntime(1193): at java.lang.reflect.Method.invoke(Method.java:511)
01-17 16:16:29.178: E/AndroidRuntime(1193): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-17 16:16:29.178: E/AndroidRuntime(1193): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-17 16:16:29.178: E/AndroidRuntime(1193): at dalvik.system.NativeStart.main(Native Method)
Here's another solution for those who need it. I had the same problem when I dragged the item to the TextView
which handled drag events. Apparently, the line in the source which causes trouble is here in the TextView.onDragEvent()
(3rd one in the following code):
case DragEvent.ACTION_DRAG_LOCATION:
final int offset = getOffsetForPosition(event.getX(), event.getY());
Selection.setSelection((Spannable)mText, offset);
return true;
The fix for me was to consume events of this type in my OnDragListener
for this TextView
:
if (dragEvent.getAction() == DragEvent.ACTION_DRAG_LOCATION) return true;
This is a bug in Android, of course, this shouldn't normally happen.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With