在Robolectric中运行时片段提交的NullPointerException

时间:2016-08-30 12:18:56

标签: android android-fragments robolectric

这是我得到的例外:

Caused by: java.lang.NullPointerException
    at android.view.WindowId.equals(WindowId.java:168)
    at android.transition.Transition.pause(Transition.java:1669)
    at android.transition.TransitionSet.pause(TransitionSet.java:483)
    at android.transition.TransitionManager.sceneChangeSetup(TransitionManager.java:311)
    at android.transition.TransitionManager.beginDelayedTransition(TransitionManager.java:415)
    at android.support.v4.app.FragmentTransitionCompat21.beginDelayedTransition(FragmentTransitionCompat21.java:67)
    at android.support.v4.app.BackStackRecord.configureTransitions(BackStackRecord.java:1257)
    at android.support.v4.app.BackStackRecord.beginTransition(BackStackRecord.java:1077)
    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:678)
    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1671)
    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:532)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at org.robolectric.shadows.ShadowMessageQueue.dispatchMessage(ShadowMessageQueue.java:130)
    at org.robolectric.shadows.ShadowMessageQueue.access$100(ShadowMessageQueue.java:30)
    at org.robolectric.shadows.ShadowMessageQueue$1.run(ShadowMessageQueue.java:95)
    at org.robolectric.util.Scheduler.runOrQueueRunnable(Scheduler.java:230)
    at org.robolectric.util.Scheduler.postDelayed(Scheduler.java:85)
    at org.robolectric.shadows.ShadowMessageQueue.enqueueMessage(ShadowMessageQueue.java:116)
    at android.os.MessageQueue.enqueueMessage(MessageQueue.java)
    at android.os.Handler.enqueueMessage(Handler.java:631)
    at android.os.Handler.sendMessageAtTime(Handler.java:600)
    at android.os.Handler.sendMessageDelayed(Handler.java:570)
    at android.os.Handler.post(Handler.java:326)
    at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1557)
    at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:654)
    at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:621)

正如您可以看到android.view.WindowId类中发生异常,所以我怀疑这是我的代码中的错误。这是我正在运行的代码:

if (AndroidHelper.isLollipopAndAbove()) {
    Fade fadeIn = new Fade(Fade.IN);
    Fade fadeOut = new Fade(Fade.OUT);
    fadeIn.setDuration(200);
    fadeOut.setDuration(200);
    fragment.setEnterTransition(fadeIn);
    fragment.setExitTransition(fadeOut);
}
FragmentTransaction fragmentTransaction = getSupportFragmentManager()
    .beginTransaction()
    .replace(R.id.container, fragment);
if (keepInStack) {
    fragmentTransaction.addToBackStack(null);
}
fragmentTransaction.commit();

如果我取出fragment.setEnterTransition(fadeIn);fragment.setExitTransition(fadeOut);

,这项工作正常

有什么方法可以解决这个问题吗?在真实设备上运行时没有问题。

编辑:只是要非常明确,这不是关于通用NullPointerException或我的片段为空或类似的问题。此问题与在Robolectric环境中运行转换代码有关。更具体地说,导致此问题的是setExitTransition,问题似乎只出现在Robolectric中。 setEnterTransition似乎工作得很好

1 个答案:

答案 0 :(得分:0)

无论您实际使用哪种测试框架,RobolectricEspressoAppium - 请。不要将动画用于测试目的。

我很确定您的问题与此非常相似:https://github.com/robolectric/robolectric/issues/1889但在这种情况下,动画会使Robolectric无休止地运行。

制作MockFragment课程,无需转换,插入器,动画师等。