App crash caused by data binding related to java.lang.Runnable android.databinding.ViewDataBinding.mRebindRunnable

时光毁灭记忆、已成空白 提交于 2021-02-09 08:59:22

问题


The exception is the following:

java.lang.NullPointerException: Attempt to read from field 'java.lang.Runnable android.databinding.ViewDataBinding.mRebindRunnable' on a null object reference
        at android.databinding.ViewDataBinding.access$100(ViewDataBinding.java:51)
        at android.databinding.ViewDataBinding$5.onViewAttachedToWindow(ViewDataBinding.java:142)
        at android.view.View.dispatchAttachedToWindow(View.java:14525)
        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2836)
        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2843)
        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2843)
        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2843)
        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2843)
        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2843)
        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2843)
        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2843)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1364)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
        at android.view.Choreographer.doCallbacks(Choreographer.java:670)
        at android.view.Choreographer.doFrame(Choreographer.java:606)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Let me try my best to make the description brief and clear.

First, this is related to a layout added dynamically. The activity's data binding works perfectly without any problem. Here is the layout foo_view:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <data>
        <variable
            name="foo"
            type="net.mydomain.myapp.MyActivity.Foo" />
        <import type="android.view.View" />
    </data>
    <!--For the sake of debugging, no data binding is actually used-->
    ...
</layout>

In MyActivity.java:

public class MyActivity extends AppCompatActivity {
    public class Foo{
       ...
    }


   ...
   addFoo(){
       FooViewBinding binding = DataBindingUtil.inflate(LayoutInflater.from(this), R.layout.foo_view, myGridLayout, true); 
       Foo foo = new Foo();
       binding.setFoo(foo);  
   }
}

The app cashes immediately upon the start of MyActivity. The crash is not caused by a specific line of code.

Could anyone shed some light on this?

[Edit] 2016-06-30

The crash happens on a Lollipop, but same code works fine on a Jelly Bean


回答1:


Just in case someone has the same problem, this errors occurs when you change the tag (i.e. view.setTag(object)) of a data-binding enabled view, as the data binding library uses the tag for itself.



来源:https://stackoverflow.com/questions/37374303/app-crash-caused-by-data-binding-related-to-java-lang-runnable-android-databindi

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