Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 610
Enver Bisevac Avatar asked Aug 11 '16 09:08

Enver Bisevac


1 Answers

Since you're overriding setContentView in your Activity, you need to 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 because I overrode setContentView in my base Activity and that fixed it.

If you overrode setContentView, getContentFrame() is the ViewGroup that contains your content, exclusive of the AppBarLayout and Toolbar. Here's an example of what getContentFrame() would look like if you had a base layout similar to what's below:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.design.widget.AppBarLayout
        ...>
        <android.support.design.widget.CollapsingToolbarLayout
           ...>
                <android.support.v7.widget.Toolbar
                   .../>
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.v7.widget.ContentFrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </android.support.v7.widget.ContentFrameLayout>
</android.support.design.widget.CoordinatorLayout>

getContentFrame() just returns the FrameLayout in the above layout.

protected ViewGroup getContentFrame() {
    return (ViewGroup) findViewById(R.id.content_frame);
}

I hope this helps.

like image 188
iRuth Avatar answered Dec 22 '22 04:12

iRuth