1

This is the exception I'm getting:

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)

As you can see the exception is happening in android.view.WindowId class, so I doubt it's an error in my code. This is the code I'm running:

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();

This works fine if I take out fragment.setEnterTransition(fadeIn); and fragment.setExitTransition(fadeOut);

Is there any way to work around this? No issue when running on a real device.

EDIT: just to be super clear, this is not an issue about a generic NullPointerException or my fragment being null or anything like that. This issue is something to do with running transition code inside Robolectric environment. To be even more specific, it's the setExitTransition that is causing this issue, and the issue seems to be only manifesting in Robolectric. setEnterTransition seems to be working just fine

vkislicins
  • 3,331
  • 3
  • 32
  • 62
  • 2
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – xenteros Aug 30 '16 at 12:24
  • @xenteros Definitely not a duplicate of 'What is a NullPointer'. If you look closer the Null Pointer is happening inside `android.view.WindowId` class – vkislicins Aug 30 '16 at 12:27
  • Looks like your `fragment` is null! could you share your code where u are initialising fragment! – Smit Aug 30 '16 at 12:38
  • @Smit My fragment is not null. Please look at the stack in the question before posting comment like that as they are not helpful. – vkislicins Aug 30 '16 at 12:39
  • Sorry about the inconvenience! But you u have mentioned that if you take out `fragment.setEnterTransition(fadeIn); and fragment.setExitTransition(fadeOut);` it works. So I assume that's the issue. – Smit Aug 30 '16 at 12:40
  • I'll update the question to make it more clear, thank. But the issue is with how the transitions are worked inside Robolectric I think, so I need a workaround. – vkislicins Aug 30 '16 at 12:42
  • @vkislicins its too late but did you find the reason for this. I have the same issue but with setEnterTransition – muditagarwal88 Nov 25 '17 at 08:57

1 Answers1

0

No matter what test framework are you actually use, Robolectric, Espresso, Appium - please. don't use animations for your test purposes.

I'm pretty sure that your problem is very similar to this: https://github.com/robolectric/robolectric/issues/1889 but in that case animations make Robolectric running endless.

Make a MockFragment class without transisions, intepolators, animators etc.

piotrek1543
  • 19,130
  • 7
  • 81
  • 94
  • You're most likely right. Can you recommend any reading/article on how to create and manage mock fragments while excluding transitions, animations etc? – vkislicins Sep 12 '16 at 10:55