gpt4 book ai didi

android拖放TextView导致异常

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:32:59 25 4
gpt4 key购买 nike

我正在尝试在 android 4+ 中使用内置的拖放功能。为此,我创建了自定义 TextView 作为

public class DragSpotTextView extends TextView

textview 拖动工作正常,但是当我将 View 拖动到某个 dragspot(这也是 textview)时,它会给出异常;

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)

最佳答案

这是为需要它的人提供的另一种解决方案。当我将项目拖到处理拖动事件的 TextView 时,我遇到了同样的问题。显然,源代码中引起问题的行在 TextView.onDragEvent() 中(以下代码中的第 3 个):

 case DragEvent.ACTION_DRAG_LOCATION:
final int offset = getOffsetForPosition(event.getX(), event.getY());
Selection.setSelection((Spannable)mText, offset);
return true;

我的解决方法是在我的 OnDragListener 中为这个 TextView 使用这种类型的事件:

 if (dragEvent.getAction() == DragEvent.ACTION_DRAG_LOCATION) return true;

这是 Android 中的一个错误,当然,这在正常情况下不应该发生。

关于android拖放TextView导致异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14377498/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com