DataBindingUtil.setContentView(this, resource) return null

前端 未结 2 2082
小鲜肉
小鲜肉 2021-01-12 04:16

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 A

相关标签:
2条回答
  • 2021-01-12 04:25

    Try to rebuild gradle and clean project

    0 讨论(0)
  • 2021-01-12 04:44

    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.

    0 讨论(0)
提交回复
热议问题