gpt4 book ai didi

android - 如果您在 onAnimationEnd 上移除子项,ViewAnimator 的 OnDraw 将抛出 NullPointerException

转载 作者:太空宇宙 更新时间:2023-11-03 13:02:04 24 4
gpt4 key购买 nike

这不是一个问题,更像是和别人分享我遇到的一个问题以及我是如何解决的。
基本上,我试图创建一个 ViewAnimator,它会创建额外的子项以响应用户点击。
为了在我对下一个 View 进行动画处理后进行清理,我将

outAnimation.setAnimationListener(listener);

并在 AnimationListener 中

public void onAnimationEnd(Animation animation) {
viewAnimator.removeView(out);
}

现在,上述方法的问题是,在 onAnimationEnd 之后,它会立即抛出 NullPointerException。基本上,这意味着 ViewAnimator 仍在使用正在动画绘制的 subview 。由于我已将其删除,因此那里为空。我已经完成了研究,基本上,这似乎是一个已知错误。请参阅:Android Animation is not finished on onAnimationEnd

为了解决这个问题,我修改了布局。

<ViewAnimator
android:id="@+id/animator"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<FrameLayout
android:id="@+id/container1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</FrameLayout>

<FrameLayout
android:id="@+id/container2"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</FrameLayout>
</ViewAnimator>

和 onAnimationEnd 我可以安全地调用 container.removeAllViews()。为了在其中设置新 View 的动画,我选择了隐藏的容器,然后

container.addView(newView);
animator.setDisplayedChild(animator.indexOfChild(container));

我很乐意看到您的意见和建议。

最佳答案

我遇到过这个问题并使用 View 的 post 方法等待动画真正完成:

      public void onAnimationEnd(Animation animation) {
//Wait until the container has finished rendering to remove the items.
view.post(new Runnable() {
@Override
public void run() {
//remove view here
}
});
}

关于android - 如果您在 onAnimationEnd 上移除子项,ViewAnimator 的 OnDraw 将抛出 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9984382/

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