无法获取用于Android数据绑定的简单文本视图

无法获取用于Android数据绑定的简单文本视图,android,data-binding,Android,Data Binding,好了,我终于可以编译我的Android数据绑定代码了。现在,它并不像我预期的那样工作。当我从我的活动调用setCurrentShopperInfo()时,我的TextView(s)不会使用我正在设置的字符串值更新 注意:我也使用了可观察字段(s)进行了尝试,得到了相同的结果 这里一定有我不明白的地方: 布局: <data> <variable name="currentShopperViewModel" type="ts.kiosk.app.checkout.v

好了,我终于可以编译我的Android数据绑定代码了。现在,它并不像我预期的那样工作。当我从我的
活动调用
setCurrentShopperInfo()
时,我的
TextView
(s)不会使用我正在设置的
字符串值更新

注意:我也使用了
可观察字段
(s)进行了尝试,得到了相同的结果

这里一定有我不明白的地方:

布局:

<data>
        <variable name="currentShopperViewModel" type="ts.kiosk.app.checkout.viewmodels.CurrentShopperViewModel" />
        <import type="android.view.View" />
    </data>


        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

             <RelativeLayout
                android:id="@+id/checkout_titlebar"
                android:layout_width="match_parent"
                android:layout_height="@dimen/app_header_height"
                android:layout_alignParentTop="true"
                android:background="@color/header_bkgrd">
                <ImageView
                    android:id="@+id/img_ts_logo"
                    android:layout_width="@dimen/app_icon_width"
                    android:layout_height="wrap_content"
                    android:adjustViewBounds="true"
                    android:scaleType="fitCenter"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="@dimen/app_icon_margin"
                    android:layout_marginRight="@dimen/app_icon_margin"
                    android:src="@drawable/touchsides_logo_notext"/>
                 <TextView
                     android:id="@+id/text_appname"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:text="@string/title_checkout"
                     android:layout_toRightOf="@+id/img_ts_logo"
                     android:layout_centerVertical="true"
                     style="@style/screenTitle"/>

                 <LinearLayout
                     android:visibility="visible"
                     android:layout_width="match_parent"
                     android:layout_height="match_parent"
                     android:orientation="horizontal"
                     android:layout_toRightOf="@+id/text_appname"
                     android:layout_toLeftOf="@+id/img_settings_menu"
                     android:paddingRight="20dip"
                     android:paddingLeft="20dip"
                     android:gravity="center_horizontal">
                     <TextView
                         android:layout_width="wrap_content"
                         android:layout_height="match_parent"
                         android:text="@{currentShopperViewModel.textShopperInfoName}"
                         style="@style/previous_transaction_titlebar"></TextView>
                     <TextView
                         android:layout_width="wrap_content"
                         android:layout_height="match_parent"
                         android:text="@{currentShopperViewModel.textShopperInfoCashback}"
                         style="@style/previous_transaction_titlebar_cashback"
                         android:layout_marginLeft="20dip"></TextView>
                     <TextView
                         android:layout_width="wrap_content"
                         android:layout_height="match_parent"
                         android:text="@{currentShopperViewModel.textShopperInfoPoints}"
                         style="@style/previous_transaction_titlebar_points"
                         android:layout_marginLeft="20dip"></TextView>
                 </LinearLayout>
mCurentShopperViewModel = new CurrentShopperViewModel(this);
        final ActivityCheckoutNewBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_checkout_new);
        binding.setCurrentShopperViewModel(mCurentShopperViewModel);
视图模型:

public class CurrentShopperViewModel extends BaseObservable{

    Context mCtx;

    //public ObservableBoolean showShopperInfo = new ObservableBoolean();


    private String textShopperInfoName;
    private String textShopperInfoCashback;
    private String textShopperInfoPoints;


    @Bindable
    public String getTextShopperInfoName() {
        return this.textShopperInfoName;
    }
    @Bindable
    public String getTextShopperInfoCashback() {
        return this.textShopperInfoCashback;
    }
    @Bindable
    public String getTextShopperInfoPoints() {
        return this.textShopperInfoPoints;
    }


    public void setTextShopperInfoName(String name) {
        this.textShopperInfoName = name;
        notifyPropertyChanged(BR.textShopperInfoName);
    }
    public void setTextShopperInfoCashback(String cashback) {
        this.textShopperInfoCashback = cashback;
        notifyPropertyChanged(BR.textShopperInfoCashback);
    }
    public void setTextShopperInfoPoints(String points) {
        this.textShopperInfoPoints = points;
        notifyPropertyChanged(BR.textShopperInfoPoints);
    }


    public CurrentShopperViewModel(Context context) {
        mCtx = context;
    }


    public void  setCurrentShopperInfo(CardStructure cardStructure ) {

        //show(true);

        int[] card = CardUtil.getInstance().getBalance(cardStructure);

        setTextShopperInfoName( String.format( "%s %s", mCtx.getResources().getString(R.string.item_wallet_desposit) , Formatter.formatCurrency(CheckoutApplication.LOCALE,card[0]) ) );
        setTextShopperInfoCashback(  String.format( "%s %s", mCtx.getResources().getString(R.string.item_wallet_desposit) , Formatter.formatCurrency(CheckoutApplication.LOCALE,card[0]) )  );
        setTextShopperInfoPoints ( String.format( "%s %s",  mCtx.getResources().getString(R.string.item_loyalty_points),card[1] ) );
    }
格拉德尔:

buildscript {
    repositories {
        mavenCentral()
        maven { url 'http://download.crashlytics.com/maven' }
        maven { url 'https://oss.sonatype.org/content/repositories/ksoap2-android-releases/' }
    }

    dependencies {
        classpath 'com.crashlytics.tools.gradle:crashlytics-gradle:1.+'
        classpath 'com.google.code.ksoap2-android:ksoap2-android:3.1.1'
    }
}



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


repositories {
    maven { url 'http://download.crashlytics.com/maven' }
    maven { url 'https://oss.sonatype.org/content/repositories/ksoap2-android-releases/' }
}

android {
    compileSdkVersion 20
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "ts.kiosk.app.checkout"
        minSdkVersion 16
        targetSdkVersion 16
        versionCode 2
        versionName "0.0.2.258"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    lintOptions {
        abortOnError false
    }


    sourceSets {
        main {
            assets.srcDirs = ['src/main/assets', 'src/main/assets/']
        }
    }


    dataBinding{
        enabled = true
    }
}



dependencies {

    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:support-v4:20.0.0'
    compile 'com.crashlytics.android:crashlytics:1.+'
    compile 'net.danlew:android.joda:2.7.2'
    compile 'com.google.code.ksoap2-android:ksoap2-android:3.1.1'
    compile 'com.squareup.retrofit:retrofit:1.6.1'
    compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0'
    //compile 'com.squareup.okhttp:okhttp:2.0.0'

    // Robolectric
    testCompile 'junit:junit:4.12'
    testCompile 'org.hamcrest:hamcrest-library:1.3'
    testCompile 'org.apache.maven:maven-ant-tasks:2.1.3' // fixes issue on linux/mac
    testCompile 'org.robolectric:robolectric:3.0'

    compile project(':asterixmodule')
    compile project(':servicemodule')
    compile project(':sdfclient')


}

android {

    packagingOptions {
        exclude 'META-INF/maven/com.squareup.okhttp/okhttp/pom.properties'
        exclude 'META-INF/maven/com.squareup.okhttp/okhttp/pom.xml'
    }
}
日志:

不确定这里有什么意义,但我在从我的
活动调用
setCurrentShopperInfo()
后看到了这一点:

04-12 21:31:30.252 866-866/ts.kiosk.app.checkout E/java.lang.StackTraceElement: InterfaceError
通知属性已更改调试

我进入了notifyPropertyChanged()方法,mCallbacks大小为0,对吗

/**
     * Notifies listeners that a specific property has changed. The getter for the property
     * that changes should be marked with {@link Bindable} to generate a field in
     * <code>BR</code> to be used as <code>fieldId</code>.
     *
     * @param fieldId The generated BR id for the Bindable field.
     */
    public void notifyPropertyChanged(int fieldId) {
        if (mCallbacks != null) {
            mCallbacks.notifyCallbacks(this, fieldId, null);
        }
    }
我还尝试了ObservaleField设置数据绑定的方法,最终称之为:

public synchronized void notifyChange() {
        if (mCallbacks != null) {
            mCallbacks.notifyCallbacks(this, 0, null);
        }
    }

mCallbacks再次为null,我认为这是一个问题,但不知道如何调试它。

我的问题是,在OnCreate()中,我调用了setContentView()两次…一次调用数据绑定,然后再次调用旧方法(显然是重写了数据绑定):


可能也值得包括gradle配置。请发布您的日志catanyone有任何线索吗?有什么可能的方法来调试这个吗?在我第一眼看到的时候没有什么明显的。老兄,我已经尝试了所有的方法,但都无法让它工作。我也有同样的问题,但只是一个片段。我“正常”地对布局进行充气,并使用数据绑定:
View-View=inflater.inflate(R.layout.my_片段,容器,false);MyFragmentBinding=MyFragmentBinding.充气(充气机、容器、假)
DataBindingUtil.setContentView(this, R.layout.activity)

//other code here

setContentView(R.layout.activity);