Android数据绑定包含布局中的“找不到符号变量” [英] 'cannot find symbol variable' in android data binding include layout
问题描述
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>
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>
MainActivity.java
LayoutMainBinding绑定= DataBindingUtil.setContentView(this,R.layout.layout_main);
setSupportActionBar(binding.content.toolbar);
Android Studio智能感知检查绑定。内容为ViewDataBinding obj
但是生成错误找不到符号变量内容
这会有问题吗?
thx!
布局 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
工具: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>
生成 ActivityMainBinding.java
。在MainActivity.java中,在 setSupportActionBar
参数中为 content
使用生成的字段:
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
ActivityMainBinding绑定= DataBindingUtil.setContentView(this,R.layout.activity_main);
setSupportActionBar(binding.content.toolbar);
}
通常,布局会为每个 android:id
和每个包含ID的包含的绑定子类。在这种情况下,数据绑定系统未检测到所包含的内容 @ layout / layout_content
是绑定布局,因此未捕获包含的Binding类。 / p>
将变量绑定到包含时,数据绑定系统将使用该值来确定所包含的布局是绑定布局。因此,如果您的布局改为:
< include layout = @ layout / layout_content
android: id = @ + id / content
app:someVar = @ {someVar} />
您将获得一个内容字段,其内容类型为 LayoutContentBinding
。这确实假定在 activity_main.xml
和 layout_content.xml 中都声明了
someVar
code>。
Android Studio中的错误指向了正确的位置,但是很难理解。将来,您可以在 app / build
目录中查找生成的绑定类。这可以帮助您弄清楚错误的含义。
我已经提交了一个错误来解决该错误-我们应该为include生成一个公共的final字段。 ID。
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>
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>
MainActivity.java
LayoutMainBinding binding = DataBindingUtil.setContentView(this,R.layout.layout_main);
setSupportActionBar(binding.content.toolbar);
Android Studio intellisense check binding.content is ViewDataBinding obj
but build error 'cannot find symbol variable content' Will this have any problem? thx!
The layout 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>
generates ActivityMainBinding.java
. In your MainActivity.java, you use the generated field for content
in the setSupportActionBar
argument:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding binding = DataBindingUtil.setContentView(this,R.layout.activity_main);
setSupportActionBar(binding.content.toolbar);
}
Normally a layout will generate public final fields for each of the Views with android:id
s and Binding subclasses for each of the includes with IDs. In this case, the data binding system did not detect that the included content @layout/layout_content
was a binding layout and thus didn't capture the Binding class for the include.
When a variable is bound to an include, the data binding system will use that to determine that the included layout is a binding layout. So, if your layout had this instead:
<include layout="@layout/layout_content"
android:id="@+id/content"
app:someVar="@{someVar}" />
You'd have gotten a content field with the type LayoutContentBinding
. This does assume that someVar
is declared in both activity_main.xml
and layout_content.xml
.
The error in Android Studio was pointing to the correct location, but it was difficult to understand. In the future, you can look for the generated binding class in your app/build
directory. This may help you figure out what the error means.
I've filed a bug to fix the error -- we should be generating a public final field for the include with the ID.
这篇关于Android数据绑定包含布局中的“找不到符号变量”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!