我正在一个片段中实现一个dragListener,如下面的代码所示:
private class ChoiceDragListener implements View.OnDragListener {
@Override
public boolean onDrag(View v, DragEvent event) {
View view = (View) event.getLocalState();
TextView dropTarget = (TextView) v;
if (view == null) {
return false;
}
final TextView dropped = (TextView) view;
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
dropped.post(new Runnable() {
@Override
public void run() {
dropped.setAlpha(0);
}
});
//
break;
// MORE CODE FOLLOWS
该实施在我的测试设备上以及大量大多数用户都可以使用。根据Google控制台,一小部分用户在行dropped.post(new Runnable(){
上有一个NullPointException,但在我添加if (view == null) {return false;}
之后消失了,这仍然是一个谜。
问题是,现在大约有相同数量的用户(在20.000个以上的用户中,少于10个)在View view = (View) event.getLocalState();
上获得ClassCastException
对此我感到困惑,因为view
只能 是TextView。更不用说,如果这仅影响极少数用户,我怀疑那里还有其他东西,也许与我的代码无关。
还是,我想知道:
我检查了其他答案,例如 Android: classCastException on view holder Android 7.0 and 7.1 getApplication() ClassCastException,但它们似乎无关。
Logcat:
Samsung Galaxy S9 (starqltesq), Android 9
java.lang.ClassCastException:
at com.****.***.***.fragmentOne$ChoiceDragListener.onDrag (fragmentOne.java:1101)
at android.view.View.callDragEventHandler (View.java:26053)
at android.view.View.dispatchDragEvent (View.java:26044)
at android.view.ViewGroup.notifyChildOfDragStart (ViewGroup.java:1827)
at android.view.ViewGroup.dispatchDragEvent (ViewGroup.java:1674)
at android.view.ViewGroup.notifyChildOfDragStart (ViewGroup.java:1827)
at android.view.ViewGroup.dispatchDragEvent (ViewGroup.java:1674)
at android.view.ViewGroup.notifyChildOfDragStart (ViewGroup.java:1827)
at android.view.ViewGroup.dispatchDragEvent (ViewGroup.java:1674)
at android.view.ViewGroup.notifyChildOfDragStart (ViewGroup.java:1827)
at android.view.ViewGroup.dispatchDragEvent (ViewGroup.java:1674)
at android.view.ViewGroup.notifyChildOfDragStart (ViewGroup.java:1827)
at android.view.ViewGroup.dispatchDragEvent (ViewGroup.java:1674)
at android.view.ViewGroup.notifyChildOfDragStart (ViewGroup.java:1827)
at android.view.ViewGroup.dispatchDragEvent (ViewGroup.java:1674)
at android.view.ViewGroup.notifyChildOfDragStart (ViewGroup.java:1827)
at android.view.ViewGroup.dispatchDragEvent (ViewGroup.java:1674)
at android.view.ViewGroup.notifyChildOfDragStart (ViewGroup.java:1827)
at android.view.ViewGroup.dispatchDragEvent (ViewGroup.java:1674)
at android.view.ViewGroup.notifyChildOfDragStart (ViewGroup.java:1827)
at android.view.ViewGroup.dispatchDragEvent (ViewGroup.java:1674)
at android.view.ViewGroup.notifyChildOfDragStart (ViewGroup.java:1827)
at android.view.ViewGroup.dispatchDragEvent (ViewGroup.java:1674)
at android.view.ViewGroup.notifyChildOfDragStart (ViewGroup.java:1827)
at android.view.ViewGroup.dispatchDragEvent (ViewGroup.java:1674)
at android.view.ViewRootImpl.handleDragEvent (ViewRootImpl.java:7423)
at android.view.ViewRootImpl.access$2100 (ViewRootImpl.java:198)
at android.view.ViewRootImpl$ViewRootHandler.handleMessage (ViewRootImpl.java:4984)
at android.os.Handler.dispatchMessage (Handler.java:106)
at android.os.Looper.loop (Looper.java:214)
at android.app.ActivityThread.main (ActivityThread.java:6981)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1445)
如果您需要更多详细信息或代码,我很乐意提供更多背景信息。