【问题标题】:Cannot find the getter for attribute 'app:rowContentText' with value type java.lang.String on com.example.widget.MyTableRow在 com.example.widget.MyTableRow 上找不到值类型为 java.lang.String 的属性“app:rowContentText”的 getter
【发布时间】:2016-09-30 02:02:03
【问题描述】:

我正在尝试在 Android 中使用双向数据绑定,但它会引发类似标题的错误。

MyTableRow 是一个自定义视图组:

public class MyShopTableRow extends RelativeLayout {

    ...

    public void setRowContentText(String text) {
        mRowInputEt.setText(text);
    }

    public String getRowContentText() {
        if(TextUtils.isEmpty(mRowInputEt.getText())){
            return "";
        }
        return mRowInputEt.getText().toString();
    }
}

然后我在 XML 文件中使用它:

<data>
    <variable
        name="shopInfo"
        type="com.example.TableModel" />
</data>

<com.example.widget.MyTableRow
        android:layout_width="match_parent"
        android:layout_height="46dp"
        android:layout_marginTop="11dp"
        ...
        app:rowInputType="text"
        app:size="regular"
        ...
        app:rowContentText="@={shopInfo.shopName}"/>

模型文件是:

public class TableModel {

    public String shopName = "lalala";
    ....
}

片段仅在单向数据绑定 (@{shopInfo.shopName}) 时有效,但如果是双向绑定则失败(找不到属性的 getter)。

我还找到了关于这个问题的question,但答案对我不起作用。

//The answer will throw an error below
//Error:(55, 17) Could not find event 'rowContentTextAttrChanged' on View type 'com.example.MyTableRow' 
@InverseBindingMethods({
    @InverseBindingMethod(type = MyShopTableRow.class, attribute = "rowContentText"),
})
public class MyShopTableRow extends RelativeLayout {
    ...
}

是IDE还是dataBinding库的bug?

【问题讨论】:

  • 错误提到了MyTableRow,但是有getter的类在MyShopTableRow上。确保在布局文件中使用 MyShopTableRow。
  • @GeorgeMount,很抱歉我的模棱两可的描述和错误的代码(那是因为我隐藏了一些敏感信息,但修改不是绝对正确的。)

标签: android data-binding


【解决方案1】:

您没有定义事件rowContentTextAttrChanged,当事件触发时,使用哪种方法获取新值。

应该是这样的

InverseBindingMethods({InverseBindingMethod(
      type = android.widget.TextView.class,
      attribute = "android:text",
      event = "android:textAttrChanged",
      method = "getText")})
  public class MyTextViewBindingAdapters { ... }

【讨论】:

猜你喜欢
  • 2018-06-20
  • 2016-01-10
  • 2016-09-16
  • 2019-11-05
  • 2012-05-22
  • 2012-06-06
  • 2015-05-12
  • 1970-01-01
相关资源
最近更新 更多