android:动画后的startDrag()上的NullpointerException

时间:2014-03-06 15:54:24

标签: android drag-and-drop nullpointerexception

我有一个自定义视图,基本上是带有Imageview和textview的FrameLayout。我在这个视图上使用了拖放功能并且完美地工作。然后我在这个视图上执行RotateAnimation。我可以再次移动此视图,但下次触摸视图时,它会消失,但会出现以下异常:

03-06 21:18:26.367: E/View(21124): Unable to initiate drag
03-06 21:18:26.367: E/View(21124): java.lang.NullPointerException
03-06 21:18:26.367: E/View(21124):  at android.view.View.startDrag(View.java:16331)
03-06 21:18:26.367: E/View(21124):  at com.example.annotations.AnnotationView.onTouchEvent(AnnotationView.java:51)
03-06 21:18:26.367: E/View(21124):  at android.view.View.dispatchTouchEvent(View.java:7392)
03-06 21:18:26.367: E/View(21124):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2229)
03-06 21:18:26.367: E/View(21124):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1964)
03-06 21:18:26.367: E/View(21124):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2235)
03-06 21:18:26.367: E/View(21124):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1936)
03-06 21:18:26.367: E/View(21124):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2235)
03-06 21:18:26.367: E/View(21124):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1936)
03-06 21:18:26.367: E/View(21124):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2235)
03-06 21:18:26.367: E/View(21124):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1936)
03-06 21:18:26.367: E/View(21124):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2235)
03-06 21:18:26.367: E/View(21124):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1936)
03-06 21:18:26.367: E/View(21124):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2235)
03-06 21:18:26.367: E/View(21124):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1936)
03-06 21:18:26.367: E/View(21124):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2177)
03-06 21:18:26.367: E/View(21124):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1482)
03-06 21:18:26.367: E/View(21124):  at android.app.Activity.dispatchTouchEvent(Activity.java:2483)
03-06 21:18:26.367: E/View(21124):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2125)
03-06 21:18:26.367: E/View(21124):  at android.view.View.dispatchPointerEvent(View.java:7577)
03-06 21:18:26.367: E/View(21124):  at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3376)
03-06 21:18:26.367: E/View(21124):  at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3308)
03-06 21:18:26.367: E/View(21124):  at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4421)
03-06 21:18:26.367: E/View(21124):  at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4399)
03-06 21:18:26.367: E/View(21124):  at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4505)
03-06 21:18:26.367: E/View(21124):  at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:178)
03-06 21:18:26.367: E/View(21124):  at android.os.MessageQueue.nativePollOnce(Native Method)
03-06 21:18:26.367: E/View(21124):  at android.os.MessageQueue.next(MessageQueue.java:125)
03-06 21:18:26.367: E/View(21124):  at android.os.Looper.loop(Looper.java:124)
03-06 21:18:26.367: E/View(21124):  at android.app.ActivityThread.main(ActivityThread.java:4949)
03-06 21:18:26.367: E/View(21124):  at java.lang.reflect.Method.invokeNative(Native Method)
03-06 21:18:26.367: E/View(21124):  at java.lang.reflect.Method.invoke(Method.java:511)
03-06 21:18:26.367: E/View(21124):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1043)
03-06 21:18:26.367: E/View(21124):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)
03-06 21:18:26.367: E/View(21124):  at dalvik.system.NativeStart.main(Native Method)

以下用于旋转视图的代码写在搜索栏的onProgressChanged()中:

                    RotateAnimation r = new RotateAnimation(mCustomView.ROTATION, progress - 180,
                            RotateAnimation.RELATIVE_TO_SELF, 0.5f,
                            RotateAnimation.RELATIVE_TO_SELF, 0.5f);
                    // r.setStartOffset(1000);
                    // r.setDuration(1000);
                    r.setFillAfter(true); // HERE
                    mCustomView.ROTATION = progress - 180;
                    mCustomView.startAnimation(r);
自定义onTouchEvent()类中的

View

@Override
public boolean onTouchEvent(MotionEvent event) {

    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        ClipData data = ClipData.newPlainText("", "");
        DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(this);
        if (data.equals(null))
            Log.d("Nully", "data + " + data.toString());
        if (shadowBuilder.equals(null))
            Log.d("Nully", "shad" + shadowBuilder.toString());
        this.startDrag(data, shadowBuilder, this, 0);
        this.setVisibility(View.INVISIBLE);
        return true;
    } else {
        return false;
    }

我使用断点调试了应用程序,并且该对象在此异常之前的匹配中似乎不为空。

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题。在您再次拖动之前,您基本上需要cancel()动画或clearAnimation()来自视图。您可以在AnimationListener的onAnimationEnd()中触发此清理。我从视图中清除动画,因为取消正在创建我视图的多个“图像”。

究竟为什么你需要这样做,我不知道。