用android 5.0 过场动画
HomeTopic topic=homeTopics.get(position); Bundle bundle = new Bundle(); bundle.putString(WebBrowseActivity._TITLE, ""); bundle.putString("shareImg",String.valueOf(view.getTag(R.id.img_id))); bundle.putString(WebBrowseActivity._CONTENT, topic.getUrl()); Intent intent=new Intent(itemView.getContext(),TopicWebBrowseActivity.class); intent.putExtras(bundle); Context context=itemView.getContext(); ActivityOptionsCompat compat= ActivityOptionsCompat.makeSceneTransitionAnimation((Activity) context, view,context.getString(R.string.share_topic));ActivityCompat.startActivity((Activity) context,intent,compat.toBundle());
偶尔抛出以下错误
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.ViewGroup.transformMatrixToGlobal(android.graphics.Matrix)' on a null object reference at android.view.GhostView.calculateMatrix(GhostView.java:95) at android.app.ActivityTransitionCoordinator$GhostViewListeners.onPreDraw(ActivityTransitionCoordinator.java:951) ....
一. 解决方法(当屏幕旋转等动作引起共享view的变化,返回可能就抛这个错,这个这么处理):
private boolean mOrientationChanged; @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); mOrientationChanged = !mOrientationChanged; } @Override public void supportFinishAfterTransition() { if (mOrientationChanged) { /** * if orientation changed, finishing activity with shared element * transition may cause NPE if the original element is not visible in the returned * activity due to new orientation, we just finish without transition here */ finish(); } else { super.supportFinishAfterTransition(); }}
二. 晚上发现问题又复现了,经过测试与调试,发现我轮播图有关。
首页轮播图,点击进去第二个activity,由于没有停止轮播,如果在二个activity页面久了,返回就会抛出异常,应该是原来共享的imageview返回时候,找不到原来第一个activity共享的imageview,就会抛错。
故解决方法。
在第一个activity onPause时候停止轮播,返回时候onResume开始轮播,这样返回共享的imageview不被回收。
public void startAutoPlay(){ mImgSwitchId.startBannerPlay(); } public void stopAutoPlay(){ mImgSwitchId.stopBannerPlay(); }