Enver Bisevac Enver Bisevac - 3 months ago 48
Android Question

DataBindingUtil.setContentView(this, resource) return null

I started using Android Data binding but without success.I have done everything as proposed in the documentation but when I have to set method value I get null.
I am using Android Studio 2.1.2 and I put in gradle

dataBinding {
enabled = true
}


in layout I do exactly da same put layout and inside I put tag data:

<data>
<variable name="order" type="com.example.Order"/>
</data>


and in code when I want to have binding variable

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
ActivityOrderOnePaneBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_order_one_pane);
binding.setOrder(mOrder);


Binding is null,I don't have compile errors.

Answer

Since you're overriding setContentView in your Activity, replace:

ActivityOrderOnePaneBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_order_one_pane);

with

ActivityOrderOnePaneBinding binding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.activity_order_one_pane, getContentFrame(), false);
setContentView(binding.getRoot());

I had the same problem and that fixed it.

Comments