【问题标题】:Cannot find the getter for attribute 'android:tag' - Android找不到属性'android:tag'的getter - Android
【发布时间】:2017-05-18 09:27:34
【问题描述】:

我正在使用数据绑定,这里我遇到了这个问题:

Error:(252, 21) Cannot find the getter for attribute 'android:tag'
with value type java.lang.String on com.hdfcfund.investor.views.EditText. 

虽然,文本属性工作正常,但在使用标签元素时出错。

<?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">
<data>

    <variable
        name="presenter"
        type="com.hdfcfund.investor.folio.step4addnominee.AddNomineePresenter" />

    <variable
        name="nominee"
        type="com.hdfcfund.investor.folio.step1.model.NewInvestorFolioRequest.Nominee" />
</data>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:clickable="true">

                <com.hdfcfund.investor.views.EditText
                    android:id="@+id/et_country"
                    style="@style/EditTextStyleRegularGrey15"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:drawableRight="@drawable/ic_arrow_input"
                    android:focusableInTouchMode="false"
                    android:hint="@string/label_country_1"
                    android:inputType="text"
                    android:onClick="@{()-> presenter.onSpinnerClick(spinnerCountry)}"
                    android:tag="@={nominee.nomineeAddress.countryCode}"
                    android:text="@={nominee.nomineeAddress.countryName}" />


</RelativeLayout>
</layout>

【问题讨论】:

    标签: android android-databinding


    【解决方案1】:

    android:tag 属性默认不支持双向绑定。这是因为没有事件机制来通知属性何时发生变化。

    您可能打算使用单向绑定:

    android:tag="@{nominee.nomineeAddress.countryCode}"
    

    用户无法更改标签值,因此无论如何,双向对那个属性的用处并不大。

    【讨论】:

      【解决方案2】:

      您需要定义@InverseBindingAdapter 以从属性返回值:

      @InverseBindingAdapter(attribute = "android:tag")
      public static String getStringTag(EditText view) {
          return String.valueOf(view.getTag());
      }
      

      【讨论】:

      • 我建议使用 String.valueOf() 而不是演员表。
      猜你喜欢
      • 2018-10-23
      • 1970-01-01
      • 2015-04-10
      • 1970-01-01
      • 2020-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-20
      相关资源
      最近更新 更多