Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'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 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!

like image 241
tatsuyuki Avatar asked Jul 27 '15 15:07

tatsuyuki


People also ask

What is Br in data binding Android?

Note: The Data Binding Library generates a class named BR in the module package which contains the IDs of the resources used for data binding. In the example above, the library automatically generates the BR. item variable.


1 Answers

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:ids 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.

like image 60
George Mount Avatar answered Oct 09 '22 20:10

George Mount