'cannot find character variable' in data binding to androids including 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 binding = DataBindingUtil.setContentView(this,R.layout.layout_main); setSupportActionBar(binding.content.toolbar); 

Android check intellisense check.content is ViewDataBinding obj

but the build error 'cannot find the contents of the character variable' Will this be a problem? THX!

+7
android data-binding
source share
1 answer

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); } 

Typically, the layout will generate public end fields for each of the views with subclasses of android:id and Binding for each of the inclusions with identifiers. In this case, the data binding system did not detect that the included content @layout/layout_content was the binding layout and thus did not capture the Binding class for include.

When a variable is bound to include, the data binding system will use this to determine that the included layout is the binding layout. So, if your layout had this:

 <include layout="@layout/layout_content" android:id="@+id/content" app:someVar="@{someVar}" /> 

You have received a field of type LayoutContentBinding . This assumes that someVar declared in both activity_main.xml and layout_content.xml .

An error in Android Studio indicated the correct location, but was difficult to understand. In the future, you can search for the generated binding class in your app/build directory. This may help you understand what the error means.

I registered an error to fix the error - we must create a public final field for include with identifier.

+8
source share

All Articles