2016-03-26 13 views
4

Ich bin während der Datenbindung Android-Tutorial fest. Ich habe herausgefunden, dass ich Probleme mit der Bindung von Daten an die Ansicht habe.Datenbindung; View-Tag ist nicht korrekt, Android

public class DataView extends FrameLayout { 
private DataViewBinding binding; 

public DataView (Context context, AttributeSet attrs) { 
    super(context,attrs); 
} 

@Override 
protected void onFinishInflate() 
{ 
    super.onFinishInflate(); 
    binding = DataViewBinding.bind(this);  <--- 
} 

@Override 
protected void onAttachedToWindow() 
{ 
    super.onAttachedToWindow(); 
    DataSource dataSource = DataSource.get("Data View"); 
    binding.setDataSource(dataSource);   <--- 
} } 

data_view.xml

<layout> 
    <data> 
     <variable 
      name="dataSource" 
      type="com.example.kaz.simplebindingdemo.DataSource" /> 
    </data> 
    <com.example.kaz.simplebindingdemo.DataView 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="16dp" 
     tools:showIn="@layout/content_main" 
     tools:context=".DataView"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:text="@{dataSource.message}" /> 
    </com.example.kaz.simplebindingdemo.DataView> 
</layout> 

Und ich bin Ausnahme in Rendering bekommen:

java.lang.RuntimeException: view tag isn't correct on view:null  at 
com.example.kaz.simplebindingdemo.databinding.DataViewBinding.bind(DataViewBinding.java:123) 
    at 
com.example.kaz.simplebindingdemo.databinding.DataViewBinding.bind(DataViewBinding.java:119) 
    at 
com.example.kaz.simplebindingdemo.DataView.onFinishInflate(DataView.java:22) 
    at 
android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:844) 
    at 
android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:70) 
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:811) at 
android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:515)  at 
android.view.LayoutInflater.inflate(LayoutInflater.java:394) 

Deletion bindende Erklärung das Problem beheben, aber ich habe keine benutzerdefinierte Ansicht bekommt. :) Alle

+0

können Sie auch prüfen, [diese] (http://chintanrathod.com/develop_apps_faster_using_data_binding_part2/) Tutorial für Ihre Referenz. –

Antwort

0

Änderung wie diese

<layout 
xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools"> 
    <data> 
     <variable 
      name="dataSource" 
      type="com.example.kaz.simplebindingdemo.DataSource" /> 
    </data> 
    <com.example.kaz.simplebindingdemo.DataView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="16dp" 
     tools:showIn="@layout/content_main" 
     tools:context=".DataView"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:text="@{dataSource.message}" /> 
    </com.example.kaz.simplebindingdemo.DataView> 
</layout>