gpt4 book ai didi

android - 带有 Robolectric 的 AnimatorProxy 中的 Nineoldandroids NullPointerException

转载 作者:行者123 更新时间:2023-11-29 00:11:17 25 4
gpt4 key购买 nike

我正在测试的 View 有动画,当我用 Robolectric 测试时它似乎出错了。动画是一个简单的滑动,使用 Nineoldandroids 以实现兼容性。在 Robolectric 之外一切正常,但是我的单元测试因空指针而崩溃:

10:34:35.419 [DEBUG] [TestEventLogger]     java.lang.NullPointerException
10:34:35.419 [DEBUG] [TestEventLogger] at com.nineoldandroids.view.animation.AnimatorProxy.applyTransformation(AnimatorProxy.java:316)
10:34:35.419 [DEBUG] [TestEventLogger] at android.view.animation.Animation.getTransformation(Animation.java:870)
10:34:35.420 [DEBUG] [TestEventLogger] at org.robolectric.shadows.ShadowView$2.run(ShadowView.java:492)
10:34:35.420 [DEBUG] [TestEventLogger] at org.robolectric.util.Scheduler.runOrQueueRunnable(Scheduler.java:218)
10:34:35.420 [DEBUG] [TestEventLogger] at org.robolectric.util.Scheduler.postDelayed(Scheduler.java:73)
10:34:35.421 [DEBUG] [TestEventLogger] at org.robolectric.shadows.ShadowChoreographer.postCallbackDelayed(ShadowChoreographer.java:44)
10:34:35.421 [DEBUG] [TestEventLogger] at android.view.Choreographer.postCallbackDelayed(Choreographer.java)
10:34:35.427 [DEBUG] [TestEventLogger] at org.robolectric.shadows.ShadowView.setAnimation(ShadowView.java:487)
10:34:35.429 [DEBUG] [TestEventLogger] at android.view.View.setAnimation(View.java)
10:34:35.431 [DEBUG] [TestEventLogger] at com.nineoldandroids.view.animation.AnimatorProxy.<init>(AnimatorProxy.java:66)
10:34:35.432 [DEBUG] [TestEventLogger] at com.nineoldandroids.view.animation.AnimatorProxy.wrap(AnimatorProxy.java:38)
10:34:35.432 [DEBUG] [TestEventLogger] at com.nineoldandroids.animation.PreHoneycombCompat$14.get(PreHoneycombCompat.java:161)
10:34:35.432 [DEBUG] [TestEventLogger] at com.nineoldandroids.animation.PreHoneycombCompat$14.get(PreHoneycombCompat.java:153)
10:34:35.432 [DEBUG] [TestEventLogger] at com.nineoldandroids.animation.PropertyValuesHolder.setupSetterAndGetter(PropertyValuesHolder.java:510)
10:34:35.433 [DEBUG] [TestEventLogger] at com.nineoldandroids.animation.ObjectAnimator.initAnimation(ObjectAnimator.java:410)
10:34:35.433 [DEBUG] [TestEventLogger] at com.nineoldandroids.animation.ValueAnimator.setCurrentPlayTime(ValueAnimator.java:538)
10:34:35.433 [DEBUG] [TestEventLogger] at com.nineoldandroids.animation.ValueAnimator.start(ValueAnimator.java:928)
10:34:35.433 [DEBUG] [TestEventLogger] at com.nineoldandroids.animation.ValueAnimator.start(ValueAnimator.java:951)
10:34:35.433 [DEBUG] [TestEventLogger] at com.nineoldandroids.animation.ObjectAnimator.start(ObjectAnimator.java:385)
10:34:35.433 [DEBUG] [TestEventLogger] at com.nineoldandroids.animation.AnimatorSet.start(AnimatorSet.java:502)

如果我在我的代码中添加一个检查以仅将 nineoldandroids 用于 API <11 并且默认情况下使用 android native 动画,测试就可以顺利通过。从 nineoldandroids 源代码中,我可以看到它在这一行上是空指针:

View view = mView.get();

其中 mView 是 mView = new WeakReference<View>(view);

关于如何从测试用例而不是我的应用代码中理想地解决这个问题有什么建议吗?

最佳答案

似乎正在发生的事情是,在 AnimationProxy 完全初始化之前,为列表元素设置动画的 Nineoldandroid 动画由 UI/Main looper 执行。这可能是由于 Robolectric 3 中更好地模拟了 Android 线程架构。

我通过调用解决了这个问题:

ShadowLooper.pauseMainLooper();

将此调用放在任何可以在 Nineoldandroids 中启动动画的方法之前。

更新:

这给我带来了更多的麻烦,上面的解决方法对于执行动画的每一段代码都很烦人,所以我 fork 并修复了它,非常小的变化:

https://github.com/apinder/NineOldAndroids/commit/6d399685dc5b3932e3b806d4d7d2f27123e1ca36

关于android - 带有 Robolectric 的 AnimatorProxy 中的 Nineoldandroids NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29966114/

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