'无法找到符号变量'在android数据绑定中包含布局

tat*_*uki 11 data-binding android

layout_content.xml

<layout>
    <android.support.design.widget.AppBarLayout
     android:id="@+id/appbar"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

     <android.support.v7.widget.Toolbar
         android:id="@+id/toolbar"
         android:layout_width="match_parent"
         android:layout_height="?attr/actionBarSize"
         android:background="?attr/colorPrimary"
          />
    </android.support.design.widget.AppBarLayout>
</layout>
Run Code Online (Sandbox Code Playgroud)

layout_main.xml

<layout>
    <android.support.v4.widget.DrawerLayout
    android:id="@+id/dl_main_drawer"
    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:fitsSystemWindows="true">

       <include layout="@layout/layout_content" android:id="@+id/content"/>

    </android.support.v4.widget.DrawerLayout>
</layout>
Run Code Online (Sandbox Code Playgroud)

MainActivity.java

LayoutMainBinding binding = DataBindingUtil.setContentView(this,R.layout.layout_main);
setSupportActionBar(binding.content.toolbar);
Run Code Online (Sandbox Code Playgroud)

Android Studio intellisense检查binding.content是ViewDataBinding obj

但是构建错误'找不到符号变量内容'这有什么问题吗?谢谢!

Geo*_*unt 13

布局activity_main.xml:

<layout>
    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context=".MainActivity">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <include layout="@layout/layout_content" android:id="@+id/content" />

        </LinearLayout>
    </android.support.v4.widget.DrawerLayout>
</layout>
Run Code Online (Sandbox Code Playgroud)

生成ActivityMainBinding.java.在MainActivity.java中,您contentsetSupportActionBar参数中使用生成的字段:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ActivityMainBinding binding = DataBindingUtil.setContentView(this,R.layout.activity_main);
    setSupportActionBar(binding.content.toolbar);
}
Run Code Online (Sandbox Code Playgroud)

通常,布局将为每个视图生成公共最终字段,其中每个视图具有android:ids和Binding子类,每个包含ID.在这种情况下,数据绑定系统没有检测到包含的内容@layout/layout_content是绑定布局,因此没有捕获包含的Binding类.

当变量绑定到include时,数据绑定系统将使用它来确定包含的布局是绑定布局.所以,如果您的布局改为:

<include layout="@layout/layout_content"
         android:id="@+id/content"
         app:someVar="@{someVar}" />
Run Code Online (Sandbox Code Playgroud)

你已经得到了一个类型的内容字段LayoutContentBinding.这并不认为someVar是在这两个声明activity_main.xmllayout_content.xml.

Android Studio中的错误指向正确的位置,但很难理解.将来,您可以在app/build目录中查找生成的绑定类.这可以帮助您找出错误的含义.

我已经提交了一个错误来修复错误 - 我们应该为包含ID的公共最终字段生成.