如何在DragEvent.ACTION_DRAG_STARTED上修​​复NPE

时间:2019-05-20 08:08:12

标签: java android android-fragments nullpointerexception

我正在一个片段中实现一个dragListener,如下面的代码所示:

private class ChoiceDragListener implements View.OnDragListener {
    @Override
    public boolean onDrag(View v, DragEvent event) {
        view = (View) event.getLocalState();
        dropTarget = (TextView) v;
        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

变量viewdropTargetdropped在类Level上声明:

private View view;
private TextView dropTarget, dropped;

实施在我的测试设备上以及对绝大多数用户有效。但是,Google控制台会通知我一些用户在dropped.post(new Runnable(){行上有NullPointException。

过去在代码的这一部分(即dragListener)中有更多的异常报告,我实现了以下解决方案: ViewGroup throwing NullPointerException in dispatchDragEvent

,即使用post(runnable)方法。这似乎减少了报告的数量。但是,仍然有一些人出现。

完整日志如下:

Samsung Galaxy J3 Pop (j3popltetfnvzw), Android 6.0

java.lang.NullPointerException: 
  at com.*******.****.******.FragmentOne$ChoiceDragListener.onDrag (FragmentOne.java:1125)
  at android.view.View.dispatchDragEvent (View.java:21164)
  at android.view.ViewGroup.notifyChildOfDrag (ViewGroup.java:1587)
  at android.view.ViewGroup.dispatchDragEvent (ViewGroup.java:1429)
  at android.view.ViewGroup.notifyChildOfDrag (ViewGroup.java:1587)
  at android.view.ViewGroup.dispatchDragEvent (ViewGroup.java:1429)
  at android.view.ViewGroup.notifyChildOfDrag (ViewGroup.java:1587)
  at android.view.ViewGroup.dispatchDragEvent (ViewGroup.java:1429)
  at android.view.ViewGroup.notifyChildOfDrag (ViewGroup.java:1587)
  at android.view.ViewGroup.dispatchDragEvent (ViewGroup.java:1429)
  at android.view.ViewGroup.notifyChildOfDrag (ViewGroup.java:1587)
  at android.view.ViewGroup.dispatchDragEvent (ViewGroup.java:1429)
  at android.view.ViewGroup.notifyChildOfDrag (ViewGroup.java:1587)
  at android.view.ViewGroup.dispatchDragEvent (ViewGroup.java:1429)
  at android.view.ViewGroup.notifyChildOfDrag (ViewGroup.java:1587)
  at android.view.ViewGroup.dispatchDragEvent (ViewGroup.java:1429)
  at android.view.ViewGroup.notifyChildOfDrag (ViewGroup.java:1587)
  at android.view.ViewGroup.dispatchDragEvent (ViewGroup.java:1429)
  at android.view.ViewGroup.notifyChildOfDrag (ViewGroup.java:1587)
  at android.view.ViewGroup.dispatchDragEvent (ViewGroup.java:1429)
  at android.view.ViewGroup.notifyChildOfDrag (ViewGroup.java:1587)
  at android.view.ViewGroup.dispatchDragEvent (ViewGroup.java:1429)
  at android.view.ViewGroup.notifyChildOfDrag (ViewGroup.java:1587)
  at android.view.ViewGroup.dispatchDragEvent (ViewGroup.java:1429)
  at android.view.ViewRootImpl.handleDragEvent (ViewRootImpl.java:6613)
  at android.view.ViewRootImpl.access$1400 (ViewRootImpl.java:221)
  at android.view.ViewRootImpl$ViewRootHandler.handleMessage (ViewRootImpl.java:4456)
  at android.os.Handler.dispatchMessage (Handler.java:102)
  at android.os.Looper.loop (Looper.java:148)
  at android.app.ActivityThread.main (ActivityThread.java:7422)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1230)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1120)

对此我感到有些茫然。对于我来说,很难解决该错误,因为它似乎只会影响相对较少的用户(并且我无法亲自复制它)。

任何想法可能是此错误的根源,还是我可以尝试的其他方法?

如果您需要更多详细信息或代码,我很乐意提供更多背景信息。

0 个答案:

没有答案