ViewDataBinding 在 activity.finish() 之后导致崩溃恢复应用程序,NPE 在视图附件期间访问 mRebindRunnable

ViewDataBinding causes crash resuming app after activity.finish(), NPE on access to mRebindRunnable during view attachment

成功启动 activity 后崩溃表面,使用数据绑定膨胀片段。在根级别按下后退按钮后,应用程序(正确地)经过 activity.finish() 但将应用程序实例保留在后台。 重新启动后,应用程序会在创建视图后的某个时刻崩溃(至少基于调试)。

崩溃:

2020-08-26 20:00:50.626 9706-9706/com.org.app.dev E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.org.app.dev, PID: 9706
    java.lang.NullPointerException: Attempt to read from field 'java.lang.Runnable androidx.databinding.ViewDataBinding.mRebindRunnable' on a null object reference
        at androidx.databinding.ViewDataBinding.access0(ViewDataBinding.java:65)
        at androidx.databinding.ViewDataBinding.onViewAttachedToWindow(ViewDataBinding.java:165)
        at android.view.View.dispatchAttachedToWindow(View.java:19564)
        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3437)
        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3437)
        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3437)
        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3437)
        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3437)
        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3437)
        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3437)
        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3437)
        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3437)
        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3437)
        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3437)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2028)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1721)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7598)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:966)
        at android.view.Choreographer.doCallbacks(Choreographer.java:790)
        at android.view.Choreographer.doFrame(Choreographer.java:725)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:951)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

深入研究源代码、调试,我能收集到的最好信息是 ViewDataBinding 发现以前绑定的视图已重新附加,并尝试重新绑定现有资源。但是,它显然丢失了对静态可运行对象的引用。来自 ViewDataBinding 的相关源代码:

    private static final OnAttachStateChangeListener ROOT_REATTACHED_LISTENER;

    static {
        if (VERSION.SDK_INT < VERSION_CODES.KITKAT) {
            ROOT_REATTACHED_LISTENER = null;
        } else {
            ROOT_REATTACHED_LISTENER = new OnAttachStateChangeListener() {
                @TargetApi(VERSION_CODES.KITKAT)
                @Override
                public void onViewAttachedToWindow(View v) {
                    // execute the pending bindings.
                    final ViewDataBinding binding = getBinding(v);
                    binding.mRebindRunnable.run();
                    v.removeOnAttachStateChangeListener(this);
                }

                @Override
                public void onViewDetachedFromWindow(View v) {
                }
            };
        }
    }

我确实从以下位置注意到了相同的堆栈跟踪:App crash caused by data binding related to java.lang.Runnable android.databinding.ViewDataBinding.mRebindRunnable,但该问题似乎与应用程序立即崩溃有关(我的问题仅在尝试补充水分后发生)。但是从那个问题来看,我确实注意到记录我的视图标签返回空值。我尝试在 xml 中设置一个静态标签,没有任何变化。

我尝试过的其他事情:

同样,只有当应用程序实例仍然存在但 activity 已被销毁时,问题才会出现。感谢您的任何提示!!

编辑:相关代码位

Activity (Java) -

ExampleFragment exampleFragment = new ExampleFragment(exampleParameter);
FragmentManager fragmentManager = getSupportFragmentManager();

fragmentManager
        .beginTransaction()
        .add(R.id.example_frame_layout, exampleFragment, EXAMPLE_TAG)
        .commit();

片段(Kotlin)-

override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
): View? {
    binding = FragmentExampleBinding.inflate(inflater, container, false).apply {
        lifecycleOwner = viewLifecycleOwner
    }
    return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    binding.run {
        viewModel = this@ExampleFragment.viewModel
    }
}

更新

有趣的是,当我尝试将相同的片段(新实例化的)膨胀到不同的(未启动的)时,我现在看到了这一点 activity。调试这种情况,除了在膨胀并返回绑定的根视图(而不是 null)后遇到崩溃之外,我找不到任何问题。我已经尝试使用为布局创建的静态方法以及 DataBindingUtil。两者都经历了同样的崩溃。

尤里卡!!

不仅 <merge/> 不能用于 <include/> 布局(也记录在此处:https://developer.android.com/topic/libraries/data-binding/expressions#includes

但是

绑定也不适用于根部有 <merge/><include/> 布局。这是它的样子:

<layout>
...

    <include layout="id/sub_layout"
        bind:viewModel="@{viewModel} />
...
</layout>

以及问题 (R.id.sub_layout.xml):

<layout>
...
    <merge>
        <TextView/>
        <ImageView/>
    </merge>
...
</layout>

奇怪的是,它似乎在启动 activity 时起作用,但很高兴知道导致此问题的症状(inflation 后异步崩溃)。 对于那些在数据绑定和错误消息方面遇到麻烦的人,我绝对建议尝试更改您的布局。