gpt4 book ai didi

Android DataBinding, View 标签在 View :null 上不正确

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:22:37 26 4
gpt4 key购买 nike

我正在尝试创建一个带有数据绑定(bind)的自定义 View 。这是自定义 View 的代码:

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 并在其中使用了上面的自定义 View :

<?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 包含在我的 Activity 布局中:

<?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) 

最佳答案

通常你会收到这个错误,因为你试图将你的 DataBinding 绑定(bind)到一个已经有另一个 DataBinding 连接到它的 View 。

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

关于Android DataBinding, View 标签在 View :null 上不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41320627/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com