java - Android 布局文件中的数据绑定(bind)错误

标签 java android data-binding

我在 Android studio 中遇到以下错误:

Error:(12, 42) error: package cloudwell.collectorapp.databinding does not exist
D:\Ismail\CollectorApp\app\src\main\java\cloudwell\collectorapp\services\RetailerViewObjectAdapter.java
D:\Ismail\CollectorApp\app\src\main\java\cloudwell\collectorapp\services\RetailerViewObjectHolder.java
Error:(7, 42) error: package cloudwell.collectorapp.databinding does not exist
Error:(14, 13) error: cannot find symbol class RetailerListBindBinding
Error:(15, 38) error: cannot find symbol class RetailerListBindBinding
**Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> java.lang.RuntimeException: Found data binding errors.
****/ data binding error ****msg:Cannot resolve type for retailer~ file:D:\Ismail\CollectorApp\app\src\main\res\layout\retailer_list_bind.xml loc:12:28 - 12:35 ****\ data binding error ******

retailer_list_bind.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools">
    <data>
        <variable name="retailer" type="cloudwell.collectorapp.services.RetailerviewObject"/>
    </data>
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{retailer.rid}"/>
        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{retailer.address}"/>
        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{retailer.name}"/>
        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{retailer.outlet}"/>
        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{retailer.phone}"/>
    </LinearLayout>
</layout>

RetailerViewObject.java

package cloudwell.collectorapp.services;
import android.databinding.BaseObservable;
import android.databinding.Bindable;

public class RetailerViewObject {
    private final String rid;
    private final String outlet;
    private final String name;
    private final String address;
    private final String phone;
    public RetailerViewObject(String rid, String outlet, String name, String address, String phone) {
        this.rid = rid;
        this.outlet = outlet;
        this.name = name;
        this.address = address;
        this.phone = phone;
    }
    public String getRid() {
        return this.rid;
    }
    public String getOutlet() {
        return this.outlet;
    }
    public String getName() {
        return this.name;
    }
    public String getAddress() {
        return this.address;
    }
    public String getPhone() {
        return this.phone;
    }
}

构建.gradle

apply plugin: 'com.android.application'
apply plugin: 'com.android.databinding'

android {
    compileSdkVersion 'Google Inc.:Google APIs:23'
    buildToolsVersion '23.0.2'
    defaultConfig {
        applicationId "cloudwell.collectorapp"
        minSdkVersion 10
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'),   'proguard-rules.pro'
        }
    }
    productFlavors {
    }
    dataBinding {
        enabled = true
    }
    useLibrary 'org.apache.http.legacy'
}

dependencies {
    //compile fileTree(include: ['*.jar'], dir: 'libs')
    //testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:recyclerview-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    compile 'com.squareup.okhttp3:okhttp:3.0.0-RC1'
    compile files('libs/volley.jar')
    compile files('libs/gcm.jar')
}

最佳答案

<data>

    <variable
        name="onDateSelect"
        type="android.view.View.OnClickListener" />

    <variable
        name="selectedDate"
        type="String" />

    <variable
        name="onSearchListener"
        type="android.view.View.OnClickListener" />


</data>

<RelativeLayout 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"
    android:id="@+id/content_vendor_billing"
    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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.xento.xapp.VendorBilling"
    tools:showIn="@layout/activity_vendor_billing">

    <android.support.design.widget.TextInputLayout
        android:id="@+id/tilSelect"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp">

        <EditText
            android:id="@+id/txtEventDate"
            style="@style/spinner_date_picker"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:focusable="false"
            android:hint="Select date"
            android:onClick="@{onDateSelect}"
            android:paddingLeft="7dp"
            android:text="@{selectedDate}"
            android:textColorHint="@color/colorAccent" />

    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout
        android:id="@+id/spnSelect"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/tilSelect"
        android:layout_marginTop="15dp">

        <Spinner
            android:id="@+id/spnVendors"
            style="@style/spinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:entries="@array/vendor"
            android:paddingTop="7dp" />

    </android.support.design.widget.TextInputLayout>

    <Button
        android:id="@+id/btnSubmit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/spnSelect"
        android:layout_marginLeft="45dp"
        android:layout_marginRight="45dp"
        android:layout_marginTop="15dp"
        android:onClick="@{onSearchListener}"
        android:text="@string/search"
        android:textAllCaps="false" />


</RelativeLayout>

<小时/>

使用 BaseObservable 扩展 POJO 类

关于java - Android 布局文件中的数据绑定(bind)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35029427/

相关文章:

android - 从特定日期/时间后捕获的 android 库中删除图像

java - 如何防止数据库关闭后线程继续运行?

android - 如何在没有 View 的情况下获取 Activity 的 windowToken?

c# - 数据绑定(bind)后 telerik radgrid 的客户端绑定(bind)未更新

java - Hibernate/Mysql,连接丢失

java - 如何修复 java.net.SocketException : Broken pipe?

javascript - 如何使用 Vue.js 将图像 url 绑定(bind)到 Framework7 list-item "media"属性?

rest - JsonMappingException : Already had POJO for id

java - 从 Android 应用程序上的任意 Activity 返回主 Activity

java - 如何从 JavaFX 中的 HTML webview 中检索组件