39

I'm getting following error when using databinding and include tag inside:

Error:Execution failed for task ':app:dataBindingProcessLayoutsBetaDebug'.>data binding error msg:Only one layout element and one data element are allowed. [path to file] has 3file:[path to file]****\ data binding error ****

This is my layout file:

[...]

        <LinearLayout
            android:id="@+id/activity_description_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:orientation="vertical">

            <include
                android:id="@+id/activity_description_header_bottom"
                layout="@layout/activity_description_header_bottom" />

            <include
                android:id="@+id/activity_description_contact_info"
                layout="@layout/activity_description_contact_info" />

            <include
                android:id="@+id/activity_description_other_info_box"
                layout="@layout/activity_description_other_info_box" />

            <include
                android:id="@+id/activity_description_bottom_buttons"
                layout="@layout/activity_description_bottom_buttons" />
        </LinearLayout>
[...]
</layout>

And in each of the included layouts i have something like this:

<layout xmlns:android="http://schemas.android.com/apk/res/android">
[...]
</layout>

From this reply: Android Data Binding using include tag i suppose that my code is correct, why databinder thinks that I use more than single tag in the file?

Community
  • 1
  • 1
RMK
  • 1,897
  • 1
  • 10
  • 17
  • 1
    Off the cuff, it would appear that `` can only integrate widgets (possibly with data binding expressions) as classic layout resources. Any `` and `` elements would have to be in the top-level resource, the one with the `` elements. That wouldn't be a surprising limitation IMHO, though it would be somewhat disappointing. – CommonsWare Apr 09 '16 at 17:14

5 Answers5

98

I solved my issue. This error appears when there is more than single element in the layout tag:

Wrong:

<layout>
     <data>
          ...
     </data>
     <LinearLayout>
          ...
     </LinearLayout>
     <LinearLayout>
          ...
     </LinearLayout>
</layout>

Correct:

<layout>
     <data>
          ...
     </data>
     <LinearLayout>
         <LinearLayout>
              ...
         </LinearLayout>
         <LinearLayout>
              ...
         </LinearLayout>
     </LinearLayout>
</layout>
RMK
  • 1,897
  • 1
  • 10
  • 17
  • If this resolves your issue, mark it as accepted so your question doesn't show up in Unanswered ;) – PLNech Jan 05 '17 at 18:39
  • 1
    Each included layout file must have `...` – Pnemonic Jan 21 '18 at 13:57
  • 1
    Wow this one's great! I built just with the `` and `` tags alone implemented, without any design elements being implemented further. And I encountered this error. Works fine after implementing design elements to the layout. So, now I've learned not to build and test binding without any design elements. – vss Nov 21 '19 at 06:40
4

In < layout>, must have one < data> and one layoutView(Relative/Linear etc).... Multiple layoutViews are not allowed,In layoutViews may have multiple layoutViews but on top layer multiplicity are not allowed....

MANI
  • 187
  • 1
  • 6
2

layout tag doesn't allow more than one child, Please put your all xml code inside any parent layout like relative/ linear layout

Example

<layout>

 <LinearLayout> 
................
............... 
</LinearLayout> 
</layout>
Md. Shofiulla
  • 2,135
  • 1
  • 13
  • 19
2
  • ViewBinding only one layout element is allowed.
  • DataBinding only one layout element and one data element are allowed.

build.gradle (:mobile)

android {
    //...

    buildFeatures{
        viewBinding = true
    }
}

Do not use android:layout_width or android:layout_height as attributes of the <layout/> tag because otherwise dataBinding already counts it as a view and then in the scope of layot we could not put another view.

test.xml

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <View
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <!-- ... -->

    </View>

</layout>

GL

Braian Coronel
  • 22,105
  • 4
  • 57
  • 62
1

Also this error hapens when you are using DataBinding and using tag without tag

correct

<layout>
 <data>
      <variable>
      </variable>
 </data>
 <LinearLayout>
      ...
 </LinearLayout>
 <LinearLayout>
      ...
 </LinearLayout>

in other words you have to keap DataBinding structure in xml

kazimad
  • 917
  • 9
  • 8