Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android Drag and Drop for TextView causing exception

Tags:

android

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)
like image 421
Ashwin Khadgi Avatar asked Jan 17 '13 10:01

Ashwin Khadgi


1 Answers

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.

like image 151
Malcolm Avatar answered Sep 20 '22 10:09

Malcolm