可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am adding content transitions to a pretty large app and in most cases I do not have any shared elements but still want to use the transition animations. I have tracked the problem down to this line of code:
ActivityOptionsCompat.makeSceneTransitionAnimation(activity, ????);
I have tried setting Pair array to null or an empty array and I have tried just leaving it out. Everything results in the following error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.ViewRootImpl.setPausedForTransition(boolean)' on a null object reference
However, I only get this error on Android 6.0+, it works fine on any version of Android 5. Am I trying to do this the wrong way?
回答1:
I did a bunch of digging in the source code and it looks like this is caused by a missed null pointer check that got fixed for Nougat.
I've got no clue how to work around it on 6.x though, unfortunately. I suppose you could add a 6.x try/catch if it's really killing you in terms of crashes, but that may also catch a bunch of other stuff you may not want it to.
回答2:
It's not an exact answer, rather a workaround; but I have managed to resolve this by postponing animations using handler.postDelayed(...) and Runnable.
You can check out the code of my solution in this question.
To be honest, it's still a lil bit too hacky for me and that's why I'm still waiting for answers with more elegant solutions. Moreover, I suppose that it only makes sense in cases similar to mine, when transitions are invoked right after creating the view.
回答3:
I faced a similar problem. The issue was that I was setting
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK)
Which killed the activity from which I was launching the transition. Removing that fixed the issue for me on Android M.