gpt4 book ai didi

android:动画后 startDrag() 出现 NullpointerException

转载 作者:行者123 更新时间:2023-11-29 01:44:41 27 4
gpt4 key购买 nike

我有一个自定义 View ,它基本上是一个带有 ImageView 和 TextView 的 FrameLayout。我在此 View 上使用了拖放功能,并且效果很好。然后我在此 View 上执行 RotateAnimation。我可以再次移动此 View ,但下次我触摸该 View 时,它会消失,但出现以下异常:

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)

以下旋转 View 的代码写在seekbar的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;
}

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

最佳答案

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

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

关于android:动画后 startDrag() 出现 NullpointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22229463/

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