11

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!

9
  • Is this with rc1 or rc0? Commented Jul 27, 2015 at 19:52
  • I use rc1. and gradle is 1.3.0-beta4 Commented Jul 28, 2015 at 0:50
  • This happens when you've renamed a layout file and the old generated Binding class is still hanging around. Try choosing "Build->Clean Project" and then recompile. Commented Jul 28, 2015 at 14:42
  • I try Clean Project and recompile,but not work. I try reopen Android studio,Also not work. Commented Jul 28, 2015 at 16:07
  • And i try create new project,but build still show 'cannot find symbol variable content' error Commented Jul 28, 2015 at 16:09

1 Answer 1

14

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Now is work,but i think this is not great solution.I hope next update <include layout="@layout/layout_content" android:id="@+id/content" /> is work.Thank you.
I reported to issue tracker here code.google.com/p/android/issues/detail?id=186914. Hope it will solve in near future, and they don't delete my that issue.
It is solved as of rc2, which went out this week. Enjoy!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.