2015-07-27 9 views
7

layout_content.xml in android Datenbindung enthalten Layout

<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

'kann nicht Symbolvariable finden'
LayoutMainBinding binding = DataBindingUtil.setContentView(this,R.layout.layout_main); 
setSupportActionBar(binding.content.toolbar); 

Android Studio Intellisense Check binding.content ist ViewDataBinding obj

aber Fehler bauen Wird dies irgendein Problem 'nicht Symbolvariable Inhalt finden können'? Danke!

+0

Ist dies mit rc1 oder rc0? –

+0

Ich benutze rc1. und Gradle ist 1.3.0-beta4 – tatsuyuki

+0

Dies passiert, wenn Sie eine Layout-Datei umbenannt haben und die alte generierte Binding-Klasse immer noch herumhängt. Wählen Sie "Build-> Clean Project" und kompilieren Sie es erneut. –

Antwort

8

Das 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> 

ActivityMainBinding.java erzeugt. In Ihrem MainActivity.java verwenden Sie das erzeugte Feld für content im setSupportActionBar Argumente:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    ActivityMainBinding binding = DataBindingUtil.setContentView(this,R.layout.activity_main); 
    setSupportActionBar(binding.content.toolbar); 
} 

Normalerweise wird ein Layout mit public final Felder für jede der Ansichten mit android:id s und Binde Subklassen für jede der umfasst generieren IDs. In diesem Fall hat das Datenbindungssystem nicht erkannt, dass der enthaltene Inhalt @layout/layout_content ein Bindungslayout war und daher die Binding-Klasse für das Include nicht erfasst.

Wenn eine Variable an ein Include gebunden ist, verwendet das Datenbindungssystem dies, um zu bestimmen, dass das enthaltene Layout ein bindendes Layout ist. Also, wenn Ihr Layout hatte diese stattdessen:

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

Sie ein Inhaltsfeld mit der LayoutContentBinding Art bekommen haben würden. Dies setzt voraus, dass someVar sowohl in activity_main.xml als auch in layout_content.xml deklariert ist.

Der Fehler in Android Studio zeigte auf den richtigen Speicherort, aber es war schwierig zu verstehen. Zukünftig können Sie in Ihrem Verzeichnis app/build nach der generierten Bindungsklasse suchen. Dies kann Ihnen helfen herauszufinden, was der Fehler bedeutet.

Ich habe einen Fehler zur Behebung des Fehlers eingereicht - wir sollten ein öffentliches Endfeld für das Include mit der ID generieren.

+0

Jetzt ist Arbeit, aber ich denke, das ist keine großartige Lösung.Ich hoffe nächstes Update '' ist Arbeit.Vielen Dank. – tatsuyuki

+0

Ich habe berichtet, Tracker hier zu veröffentlichen https://code.google.com/p/android/issues/detail?id=186914. Ich hoffe, es wird in naher Zukunft gelöst werden, und sie löschen mein Problem nicht. – KimKha

+1

Es ist gelöst von RC2, die diese Woche ausging. Genießen! –

Verwandte Themen