布局文件Android中的数据绑定错误

时间:2016-01-27 05:19:39

标签: 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;
    }
}

的build.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')
}

1 个答案:

答案 0 :(得分:0)

<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类