Android DataBinding,视图标记在视图上不正确:null

时间:2016-12-25 11:09:14

标签: android android-databinding

我正在尝试使用数据绑定创建自定义视图。以下是自定义视图的代码:

package com.xxx.myapplication;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.util.AttributeSet;
import android.widget.FrameLayout;

import com.xxx.myapplication.databinding.DataviewBinding;

/**
 * Created by atp on 12/25/2016.
*/
public class DataView extends FrameLayout {
    DataviewBinding binding;

    public DataView(Context context) {
        super(context);
    }

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

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

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

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

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        Customer customer = new Customer();
        customer.firstName = "Custom view ";
        binding.setDataSource(customer);
    }
}

我创建了一个布局文件dataview.xml,并在其中使用了上面的自定义视图:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:tools="http://schemas.android.com/tools">
    <data>
        <variable
            name="dataSource"
            type="com.xxx.myapplication.Customer" />
    </data>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <com.xxx.myapplication.DataView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@{dataSource.firstName}" />
        </com.xxx.myapplication.DataView>
    </LinearLayout>
</layout>

然后我在我的活动布局中包含了dataview.xml:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:bind="http://schemas.android.com/apk/res-auto">

    <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin">

        <include layout="@layout/dataview"/>
    </RelativeLayout>
</layout>

但是当我运行程序时,我得到以下异常:

java.lang.RuntimeException: Unable to start activity ComponentInfo{---}:android.view.InflateException: Binary XML file line #13: view tag isn't correct on view:null
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2464)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524)
at android.app.ActivityThread.access$900(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:234)
at android.app.ActivityThread.main(ActivityThread.java:5526)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.view.InflateException: Binary XML file line #13: view tag isn't correct on view:null
at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.xxx.myapplication.MainActivity.onCreate(MainActivity.java:16)
at android.app.Activity.performCreate(Activity.java:6285)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524) 
at android.app.ActivityThread.access$900(ActivityThread.java:154) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:234) 
at android.app.ActivityThread.main(ActivityThread.java:5526) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: java.lang.RuntimeException: view tag isn't correct on view:null
at com.xxxx.myapplication.databinding.DataviewBinding.bind(DataviewBinding.java:128)
at com.xxx.myapplication.databinding.DataviewBinding.bind(DataviewBinding.java:124)
at com.xxx.myapplication.DataView.onFinishInflate(DataView.java:35)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:844)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:838)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:971)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:831)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284) 
at  android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
at com.xxx.myapplication.MainActivity.onCreate(MainActivity.java:16) 
at android.app.Activity.performCreate(Activity.java:6285) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524) 
at android.app.ActivityThread.access$900(ActivityThread.java:154) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391)     
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:234) 
at android.app.ActivityThread.main(ActivityThread.java:5526) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

5 个答案:

答案 0 :(得分:9)

当我在不同的模块中有多个具有相同名称的布局时,我遇到了同样的问题,但是在膨胀时我引用的是没有数据绑定的那个(不是以layout标签开头的那个)。

答案 1 :(得分:7)

通常您会收到此错误,因为您尝试将DataBinding绑定到已连接了另一个DataBinding的视图。

为了确保没有任何内容绑定到您的视图,请尝试DataBindingUtils.getBinding(view)。它将返回视图的当前DataBinding,因此您可以重用它而不是尝试创建新的DataBinding。

答案 2 :(得分:4)

现在已经在数据绑定中修复了一个错误,其中通胀期间的绑定会导致问题。如果你将绑定移动到通胀步骤之后(不幸的是,onFinishInflate()仍然计入充气期间),那么你将避免这个错误。这应该在Android Studio 2.3中修复。

答案 3 :(得分:1)

我认为您需要将以下命名空间添加到 dataview.xml : 的的xmlns:应用=&#34; HTTP://schemas.android.com/apk/res-auto"

这样就变成了这样:

<?xml version="1.0" encoding="utf-8"?>
<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">
    <data>
        <variable
            name="dataSource"
            type="com.xxx.myapplication.Customer" />
    </data>
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <com.xxx.myapplication.DataView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@{dataSource.firstName}" />
        </com.xxx.myapplication.DataView>
    </LinearLayout>
</layout>

答案 4 :(得分:0)

其中一个错误可能是由于您的应用程序中有一个库模块引起的,并且您仅在库模块中使用了数据绑定,请确保您的应用程序模块中的布局文件与库模块中的布局文件不同。就我而言,我在应用程序模块中使用fragment_home.xml(不使用数据绑定),而在库模块中使用fragment_home.xml(使用数据绑定)重命名解决了这个问题。