Conent Transition NPE with empty shared elements

匿名 (未验证) 提交于 2019-12-03 08:41:19

问题:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!