【问题标题】:Cannot start animator on a detached view无法在分离视图上启动 animator
【发布时间】:2016-10-02 12:40:16
【问题描述】:

我在 Kotlin 中创建了扩展 PopupView 的类,并尝试使用 CircleReveal 库对其进行动画处理。这是我班级的功能

fun show(root: View) {
    showAtLocation(root, Gravity.CENTER, 0, 0)

    val cx = (mainView.left + mainView.right) / 2
    val cy = (mainView.top + mainView.bottom) / 2
    val dx = Math.max(cx, mainView.width - cx)
    val dy = Math.max(cy, mainView.height - cy)
    val finalRadius = Math.hypot(dx.toDouble(), dy.toDouble()).toFloat()

    with (ViewAnimationUtils.createCircularReveal(mainView, cx, cy, 0f, finalRadius)) {
        interpolator = AccelerateDecelerateInterpolator()
        duration = 1500
        start()
    }
}

该代码给了我以下错误

java.lang.IllegalStateException: Cannot start this animator on a detached view!
                                                            at android.view.RenderNode.addAnimator(RenderNode.java:817)
                                                            at android.view.RenderNodeAnimator.setTarget(RenderNodeAnimator.java:300)
                                                            at android.view.RenderNodeAnimator.setTarget(RenderNodeAnimator.java:282)
                                                            at android.animation.RevealAnimator.<init>(RevealAnimator.java:37)
                                                            at android.view.ViewAnimationUtils.createCircularReveal(ViewAnimationUtils.java:53)
                                                            at io.codetail.animation.ViewAnimationUtils.createCircularReveal(ViewAnimationUtils.java:74)
                                                            at io.codetail.animation.ViewAnimationUtils.createCircularReveal(ViewAnimationUtils.java:39)

类初始化

class MemberMenu(ctx: Context, val member: Member): PopupWindow(ctx), View.OnClickListener {
val mainView: View

init {
    contentView = LayoutInflater.from(ctx).inflate(R.layout.member_menu_layout, null)
    mainView = contentView.findViewById(R.id.member_menu_view)
    val size = Helpers.dipToPixels(ctx, 240f)
    width = size; height = size
    setBackgroundDrawable(ColorDrawable())
    isOutsideTouchable = true
    isTouchable = true
}
.......

【问题讨论】:

  • 你是如何初始化mainView的?
  • @Kiskae 更新问题

标签: android kotlin


【解决方案1】:

不确定,如果它是正确的解决方案,但我只是将该代码移至OnAttachStateChangeListener

    fun show(root: View) {
        showAtLocation(root, Gravity.CENTER, 0, 0)
        backgroundView.addOnAttachStateChangeListener(this)
    }

    override fun onViewAttachedToWindow(v: View?) {
        if (v==null) return

        with(ViewAnimationUtils.createCircularReveal(v, 500, 500, 0f, 500f)) {
            interpolator = AccelerateDecelerateInterpolator()
            duration = 2500
            start()
        }
    } override fun onViewDetachedFromWindow(v: View?) {}

【讨论】:

    【解决方案2】:

    我遇到了同样的崩溃,甚至我使用的是同一个库,问题是设备的“不保持活动”标志为 ON,这就是它崩溃的原因。

    解决方案

    转到-->设置-->开发者选项-->不要保持活动(关闭)

    注意:开发者选项路径可能因设备而异

    希望对你有所帮助。

    【讨论】:

    • "Don't Keep Activity" 用于实际重现此错误。无论如何,这有时会在生产中发生,即使它已关闭。这不是解决方案,而是“它现在可以在我的手机上使用”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-12
    • 1970-01-01
    相关资源
    最近更新 更多