Reputation: 322
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)
Upvotes: 5
Views: 1663
Reputation: 5011
I had the same problem. I fixed it by creating a custom TextView
and making sure I consumed the DragEvent.ACTION_DRAG_LOCATION
event myself and let the parent class handle the other Drag events, like so:
public class CustomTextView extends TextView {
@Override
public boolean onDragEvent(DragEvent event) {
if(event.getAction() == DragEvent.ACTION_DRAG_LOCATION) {
return true;
} else {
return super.onDragEvent(event);
}
}
}
Hope it helps!
Upvotes: 1
Reputation: 41510
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.
Upvotes: 5
Reputation: 2878
For me the app crashed when my drag shadow, which was not a TextView, entered a drop zone that was a TextView.
My solution in this case was to wrap the drop TextView with a FrameLayout (I'm willing to bet that any *Layout would work) - not the most elegant solution performance-wise, but not the worst either :)
Upvotes: 0
Reputation: 2427
I had the same problem. I read this and then tried replacing the following line of code:
ClipData data = ClipData.newPlainText("myLabel", "myString");
with:
ClipData data = ClipData.newPlainText("myLabel", ((TextView) v).getText());
Apparently in the onDragEvent method, the second parameter is cast to Spannable, which throws an exception when using a String.
Upvotes: 0
Reputation: 322
Earlier I was using anonymous inline class for dragListener. Now I changed it and implement OnDragListener at class level. This way I solved it.
Upvotes: 0