Get binding from view class
<p>I have a CustomView class with a databound layout that takes a variable. In the layout that contains the CustomView, I want to pass an attribute into the CustomView, and have that CustomView pass that attribute into its own layout's binding. Here's what I have:</p>

<pre><code>public class CustomView extends LinearLayout
{
public CustomView(Context inContext, AttributeSet inAttrs)
{
    super(inContext, inAttrs);

    inflate(inContext, R.layout.custom_view, null);
}



@BindingAdapter({"app:variable"})
public static void SetVariable(CustomView inCustomView, VariableType inMyVariable)
{
    CustomViewBinding binding = DataBindingUtil.getBinding(inCustomView);

    binding.setMyVariable(inMyVariable);
}
}
</code></pre>

<p>This crashes trying to extract the binding from the view. Is this even possible? Here is the stack trace:</p>

<pre><code>java.lang.NullPointerException: Attempt to invoke virtual method 'void xxx.databinding.CustomViewBinding.setVariableType(xxx.VariableType)' on a null object reference
                                                                            at xxx.CustomView.SetDynamicList(CustomView.java:32)
                                                                            at xxx.MyFragmentBinding.executeBindings(MyFragmentBinding.java:116)
                                                                            at android.databinding.ViewDataBinding.executePendingBindings(ViewDataBinding.java:350)
                                                                            at android.databinding.ViewDataBinding$6.run(ViewDataBinding.java:167)
                                                                            at android.databinding.ViewDataBinding$7.doFrame(ViewDataBinding.java:233)
                                                                            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:856)
                                                                            at android.view.Choreographer.doCallbacks(Choreographer.java:670)
                                                                            at android.view.Choreographer.doFrame(Choreographer.java:603)
                                                                            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)
</code></pre>

<p>And if I change</p>

<pre><code>DataBindingUtil.getBinding(inCustomView)
</code></pre>

<p>to</p>

<pre><code>DataBindingUtil.bind(inCustomView)
</code></pre>

<p>then I get this:</p>

<pre><code>java.lang.IllegalArgumentException: View is not a binding layout
                                                                            at android.databinding.DataBindingUtil.bind(DataBindingUtil.java:166)
                                                                            at android.databinding.DataBindingUtil.bind(DataBindingUtil.java:140)
                                                                            at xxx.CustomView.SetDynamicList(CustomView.java:30)
                    -                                                        at xxx.databinding.MyFragmentBinding.executeBindings(MyFragmentBinding.java:116)
                                                                            at android.databinding.ViewDataBinding.executePendingBindings(ViewDataBinding.java:350)
                                                                            at android.databinding.ViewDataBinding$6.run(ViewDataBinding.java:167)
                                                                            at android.databinding.ViewDataBinding$7.doFrame(ViewDataBinding.java:233)
                                                                            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:856)
                                                                            at android.view.Choreographer.doCallbacks(Choreographer.java:670)
                                                                            at android.view.Choreographer.doFrame(Choreographer.java:603)
                                                                            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)
</code></pre>

<p>This might imply that the layout file is not formatted for databinding, but it is. It has the layout element, and the data element with variables and everything.</p>