49

I'm using Activity transitions from a ViewPager (in the calling activity) with a shared element and content transitions as well. I'm getting this crash when re-entering to the calling activity:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.os.ResultReceiver.send(int, android.os.Bundle)' on a null object reference
  at android.app.EnterTransitionCoordinator.sendSharedElementDestination(EnterTransitionCoordinator.java:199)
  at android.app.EnterTransitionCoordinator.viewsReady(EnterTransitionCoordinator.java:123)
  at android.app.EnterTransitionCoordinator$2.onPreDraw(EnterTransitionCoordinator.java:148)
  at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:895)
  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2153)
  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1180)
  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6558)
  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
  at android.view.Choreographer.doCallbacks(Choreographer.java:590)
  at android.view.Choreographer.doFrame(Choreographer.java:560)
  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
  at android.os.Handler.handleCallback(Handler.java:739)
  at android.os.Handler.dispatchMessage(Handler.java:95)
  at android.os.Looper.loop(Looper.java:145)
  at android.app.ActivityThread.main(ActivityThread.java:5832)
  at java.lang.reflect.Method.invoke(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:372)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)

Also, Once going back, the screen begins to flicker continuously with a white screen flashing in and out.

Here are my Transition flags:

<item name="android:windowContentTransitions">true</item>
<item name="android:windowActivityTransitions">true</item>
<item name="android:windowAllowReturnTransitionOverlap">false</item>

I tried setting Enter/Exit transitions both on the Calling and Called activity but no luck.

Alex M
  • 2,756
  • 7
  • 29
  • 35
OferM
  • 902
  • 1
  • 8
  • 20
  • Wow, that's a new one. Ideally, you'd file a bug on this with an app that can reproduce it. It appears the the exiting activity set the ResultReceiver to null somehow. If you can't solve this in any other way, you can use finish() instead of finishAfterTransition() to avoid the activity transition, but it is quite strange. Is it possible that the exiting activity is being killed as the reentering activity is being launched? – George Mount Dec 11 '15 at 03:10
  • @GeorgeMount don't think so. If A is the calling activity and B is the called activity then my logs when entering B are as follows: A: onPause B: onCreate B: onStart B: onResume B: onSaveInstanceState A: onStop – OferM Dec 13 '15 at 08:05
  • And then when re-entering it should ideally be: A: onStart (crash is here) B: onPause A: onResume B: onStop B: onDestroy – OferM Dec 13 '15 at 09:06
  • I've also noticed that the re-enter transition works for the first 2 fragments in the ViewPager and crashes consistently for the rest of the items. The ViewPager always holds the same fragment, so it's not a specific fragment issue. – OferM Dec 13 '15 at 09:31
  • Looks like a weird timing issue. I haven't reproduced it, but you can try calling postponeEnterTransition() in activity A's onActivityReenter and then wait for the ViewPager to finish loading its contents (all fragments are loaded) before calling startPostponedEnterTransition(). I'd really like a reproducible test case so that I can fix it in the framework. – George Mount Dec 16 '15 at 18:37
  • @GeorgeMount I'll try to recreate a sample app and file a bug, but it will probably take me some time.. – OferM Dec 20 '15 at 14:44
  • 1
    Noticed same issue occurring sometimes in my app when transitioning back from detail screen to collection screen having viewpager and both the screens have shared imageview showing transition animation. – Ankur Dec 30 '15 at 09:12
  • Kindly check. I hope it will helpful https://developer.android.com/training/transitions/start-activity#java – sivaprakash Jan 29 '19 at 16:57

2 Answers2

1

Try to get fragment from FragmentManager:

fragmentManager.findFragmentByTag("android:switcher:" + R.id.viewPager + ":" + position)

If fragment is null, try creating a new fragment.

Talha Tayyab
  • 8,111
  • 25
  • 27
  • 44
FRIST_008
  • 85
  • 6
0

Activity transitions with shared elements can sometimes result in crashes due to various reasons. Here are a few common causes of crashes and how to avoid them:

Timing issues: Make sure that the shared elements have been properly sized and laid out before the transition begins. Delaying the transition start until after the shared elements have been fully initialized can prevent crashes.

Inconsistent names: If you are using shared element transitions between activities, make sure that the names of the shared elements in both activities are the same. Mismatched names can result in a crash.

Improper use of View.setTransitionName(): When using shared element transitions, it's important to set a unique transition name for each shared element. If two shared elements have the same transition name, a crash can occur.

OutOfMemoryErrors: Large images or Bitmaps used as shared elements can cause OutOfMemoryErrors, resulting in a crash. To avoid this, make sure to resize the images or Bitmaps to a smaller size before using them as shared elements.

Missing transition in XML: If the transition between the two activities is not defined in the XML, a crash can occur. Make sure that the transition is properly defined in the XML and that the correct transition is being used for the shared elements.

Here's an best example of avoid timing issues when using shared elements:

private void startActivityWithSharedElement(Intent intent) {
    final ImageView sharedImageView = findViewById(R.id.shared_image_view);

    sharedImageView.getViewTreeObserver().addOnPreDrawListener(

        new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {

         sharedImageView.getViewTreeObserver().removeOnPreDrawListener(this);
         startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(
                                MainActivity.this, sharedImageView, "shared_image").toBundle());
                return true;
            }
        });
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197