简体   繁体   English

数据绑定; 视图标签不正确,Android

[英]data binding; view tag isn't correct, Android

I'm stuck during Data Binding Android tutorial.我在数据绑定 Android 教程中被卡住了。 I figured out that I have problem with binding data to the view.我发现我在将数据绑定到视图时遇到了问题。

 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数据视图.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>

And in rendering I'm getting exception:在渲染中,我遇到了异常:

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 binding declaration fix the problem but I have got no custom view.删除绑定声明解决了这个问题,但我没有自定义视图。 :) Any :) 任何

change like this像这样改变

<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>
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"

These attributes should be in <layout> tags at the top这些属性应该在顶部的<layout>标签中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM