运行Android 8.0的Samsung设备上的共享元素过渡崩溃

时间:2018-11-23 17:06:42

标签: android kotlin samsung-mobile android-transitions shared-element-transition

我发生了一次奇怪的崩溃,在仅运行Android 8.0的三星设备上似乎无法修复。

我在一个启动活动的Fragment中填充了一个recyclerView。

这是我的ItemHolder布局

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="@dimen/table_card_header_size"
android:transitionName="@string/team_profile_transition"
android:id="@+id/tableRowMainContainer"
android:background="@drawable/rounded_rect_top"
> ...

这是我与共享元素进行活动的目的

private val teamProfileOnClick = View.OnClickListener { v ->
    val teamContainer = v.findViewById<FrameLayout>(R.id.tableRowMainContainer)
    val tag = v.tag as TableRow
    val intent = Intent(context, TeamProfileActivity::class.java)
    intent.putExtra(TeamProfileActivity.TEAM_ID_PROFILE, tag.teamId)
    intent.putExtra(TeamProfileActivity.TEAM_NAME, tag.team.name)
    intent.putExtra(TeamProfileActivity.TEAM_COMPETITION_ID, tableCompetitionId)
    intent.putExtra(TeamProfileActivity.TEAM_TYPE, tag.team.teamType)

    val options = ActivityOptionsCompat.makeSceneTransitionAnimation(context as Activity,
            teamContainer,
            ViewCompat.getTransitionName(teamContainer))
    context.startActivity(intent, options.toBundle())
}

这是目标活动的布局

<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tool="http://schemas.android.com/tools"
android:id="@+id/cardview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:transitionName="@string/team_profile_transition"
app:cardCornerRadius="10dp"
> ...

这是崩溃日志

java.lang.NullPointerException: 
  at android.app.ActivityTransitionCoordinator.setSharedElementState (ActivityTransitionCoordinator.java:553)
  at android.app.ActivityTransitionCoordinator.setSharedElementState (ActivityTransitionCoordinator.java:653)
  at android.app.EnterTransitionCoordinator.startSharedElementTransition (EnterTransitionCoordinator.java:428)
  at android.app.EnterTransitionCoordinator.-wrap4 (Unknown Source)
  at android.app.EnterTransitionCoordinator$3.lambda$-android_app_EnterTransitionCoordinator$3_18867 (EnterTransitionCoordinator.java:492)
  at android.app.-$Lambda$CsyQO--8YdRe5wlajUCi-L98enA$1.$m$0 (Unknown Source:8)
  at android.app.-$Lambda$CsyQO--8YdRe5wlajUCi-L98enA$1.run (Unknown Source)
  at android.app.ActivityTransitionCoordinator.startTransition (ActivityTransitionCoordinator.java:902)
  at android.app.EnterTransitionCoordinator$3.lambda$-android_app_EnterTransitionCoordinator$3_18819 (EnterTransitionCoordinator.java:491)
  at android.app.-$Lambda$CsyQO--8YdRe5wlajUCi-L98enA$2.$m$0 (Unknown Source:8)
  at android.app.-$Lambda$CsyQO--8YdRe5wlajUCi-L98enA$2.run (Unknown Source)
  at com.android.internal.view.OneShotPreDrawListener.onPreDraw (OneShotPreDrawListener.java:78)
  at android.view.ViewTreeObserver.dispatchOnPreDraw (ViewTreeObserver.java:1045)
  at android.view.ViewRootImpl.performTraversals (ViewRootImpl.java:2800)
  at android.view.ViewRootImpl.doTraversal (ViewRootImpl.java:1779)
  at android.view.ViewRootImpl$TraversalRunnable.run (ViewRootImpl.java:7810)
  at android.view.Choreographer$CallbackRecord.run (Choreographer.java:911)
  at android.view.Choreographer.doCallbacks (Choreographer.java:723)
  at android.view.Choreographer.doFrame (Choreographer.java:658)
  at android.view.Choreographer$FrameDisplayEventReceiver.run (Choreographer.java:897)
  at android.os.Handler.handleCallback (Handler.java:789)
  at android.os.Handler.dispatchMessage (Handler.java:98)
  at android.os.Looper.loop (Looper.java:164)
  at android.app.ActivityThread.main (ActivityThread.java:6938)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.Zygote$MethodAndArgsCaller.run (Zygote.java:327)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1374)

2 个答案:

答案 0 :(得分:0)

我找到的解决方法如下: 打开共享元素时,冻结/取消冻结recyclerView:

 override fun onPause() {
    super.onPause()
    tableCardStack.isLayoutFrozen = true
}

override fun onResume() {
    super.onResume()
    tableCardStack.isLayoutFrozen = false
}

我注意到的是,我正在制作动画的视图没有动画回到正确的位置。所以我想冻结布局。我知道这不是最好的解决方案,但是我找不到更好的方法

答案 1 :(得分:0)

看起来某些Samsung设备不会检查父视图是否为null,这在某些情况下可能会发生。您可以通过从过渡中移除没有父项的视图来防止发生崩溃。这是通过在活动中添加SharedElementCallback并覆盖onSharedElementEnd和/或onSharedElementStart(取决于您的Enter或Exit转换是否崩溃)来完成的,然后查找并删除视图像这样:

activity.setEnterSharedElementCallback(object : SharedElementCallback() {

    override fun onSharedElementStart(sharedElementNames: MutableList<String>?, sharedElements: MutableList<View>?, sharedElementSnapshots: MutableList<View>?) {
        sharedElements?.removeAll(sharedElements.filter { it.parent == null })
        super.onSharedElementStart(sharedElementNames, sharedElements, sharedElementSnapshots)
    }

    override fun onSharedElementEnd(sharedElementNames: MutableList<String>?, sharedElements: MutableList<View>?, sharedElementSnapshots: MutableList<View>?) {
        sharedElements?.removeAll(sharedElements.filter { it.parent == null })
        super.onSharedElementEnd(sharedElementNames, sharedElements, sharedElementSnapshots)
    }
}

请注意,这不能消除崩溃的根本原因,只能通过在没有父级的情况下不对视图进行动画处理来防止崩溃发生。