DataBindingUtil.setContentView(this,resource)返回null [英] DataBindingUtil.setContentView(this, resource) return null

查看:176
本文介绍了DataBindingUtil.setContentView(this,resource)返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用Android数据绑定,但没有成功.我已经完成了文档,但是当我必须设置方法值时,我会得到null. 我正在使用Android Studio 2.1.2,并且已放入gradle

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.

推荐答案

由于您要覆盖Activity中的setContentView,因此需要替换:

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

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

使用

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

我遇到了同样的问题,因为我在基础Activity中覆盖了setContentView并修复了该问题.

I had the same problem because I overrode setContentView in my base Activity and that fixed it.

如果您覆盖setContentView,则getContentFrame()是包含内容的ViewGroup,不包括AppBarLayoutToolbar.这是一个getContentFrame()的示例,如果您的基本布局类似于以下内容:

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()仅返回上述布局中的FrameLayout.

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

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

我希望这会有所帮助.

这篇关于DataBindingUtil.setContentView(this,resource)返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆