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!

rekire
  • 47,260
  • 30
  • 167
  • 264
tatsuyuki
  • 197
  • 1
  • 11
  • Is this with rc1 or rc0? – George Mount Jul 27 '15 at 19:52
  • I use rc1. and gradle is 1.3.0-beta4 – tatsuyuki Jul 28 '15 at 00: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. – George Mount Jul 28 '15 at 14:42
  • I try Clean Project and recompile,but not work. I try reopen Android studio,Also not work. – tatsuyuki Jul 28 '15 at 16:07
  • And i try create new project,but build still show 'cannot find symbol variable content' error – tatsuyuki Jul 28 '15 at 16:09
  • I tested the code you have and it compiled fine for me. The error normally comes from the generated code. Android Studio will go to the place where the error is. Can you edit your question and add the code (+/- ~20 lines) that is generating the error and indicate which line is generating the error? Also, if the error isn't coming from LayoutContentBinding.java or LayoutMainBinding.java, please add the layout that is related to the binding as well. You can also file a bug in the bug tracker and post your project there. – George Mount Jul 28 '15 at 19:42
  • I upload my test project in [gitHub](https://github.com/tatsuyuki25/AndroidTestDataBinding) – tatsuyuki Jul 29 '15 at 01:21
  • I have no idea this code where wrong – tatsuyuki Jul 29 '15 at 01:24
  • Android studio show error is '...\testdatabinding\MainActivity.java Error:(17, 16) error: cannot find symbol variable content' – tatsuyuki Jul 29 '15 at 01:28

1 Answers1

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.

George Mount
  • 20,708
  • 2
  • 73
  • 61
  • Now is work,but i think this is not great solution.I hope next update `` is work.Thank you. – tatsuyuki Jul 30 '15 at 01:50
  • I reported to issue tracker here https://code.google.com/p/android/issues/detail?id=186914. Hope it will solve in near future, and they don't delete my that issue. – KimKha Sep 18 '15 at 15:49
  • 1
    It is solved as of rc2, which went out this week. Enjoy! – George Mount Sep 18 '15 at 19:56