2017-09-27 85 views
1

我在我的項目中有多個模塊,比如模塊應用程序和模塊A,它們充當模塊應用程序的庫。我使用數據綁定,並通過在各模塊的build.gradle加入Android數據綁定包含來自不同模塊的佈局

dataBinding { enabled = true }

工作正常。

當我使用標籤包含模塊A的佈局時發生問題。當我試圖訪問包含佈局的數據綁定,它返回查看對象,而不是ViewDataBinding

Android studio autocomplete

然而,當我試圖繼續訪問所包含的佈局裏面的ID,彙編工作正常即使IDE顯示錯誤。我試過重建項目,無效緩存和其他一切。

Error on android studio

而且我敢肯定,已經遵循瞭如何實現數據綁定的規則。這是在模塊應用的佈局:

<layout xmlns:android="http://schemas.android.com/apk/res/android> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@color/light_gray" 
     android:orientation="vertical"> 

     <include layout="@layout/included_layout" id="@+id/contact"/> 
    </LinearLayout> 
</layout> 

而且對模塊A的佈局:

<layout xmlns:android="http://schemas.android.com/apk/res/android> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <TextView 
      android:id="@+id/tv_text" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"/> 
    </LinearLayout> 
</layout> 
+0

IDE顯示什麼錯誤? –

+0

無法解析符號,基本上找不到它https://i.stack.imgur.com/afIQX.png – luthfihariz

回答

0

很可能是一個建造順序問題。您可能需要確保模塊A是在父模塊嘗試訪問其內容之前構建的。

你在綁定中看到的是您分配給聯繫人的ID。這很好,因爲它找到了您嘗試訪問的佈局。接下來它會查找聯繫人的綁定文件。基於你的命名約定,應該是類似於IncludedLayoutBinding的東西。該文件將嵌套的textViews作爲tv_text。

現在,如果您沒有看到tv_text,這是因爲從未爲子模塊創建了IncludedLayoutBinding。

您確定您的子模塊已正確啓用了數據綁定,並且確定它在您嘗試訪問其值之前已經構建好了嗎?

您可以檢查childModule/build/source/dataBinding文件夾,以確保您看到您創建的layoutBinding類。我猜目前它不在。