与视图属性的数据绑定错误

时间:2016-09-03 19:56:07

标签: android data-binding attributes

也许有人可以向我解释为什么这不起作用以及我如何解决它。

xml查看文件的片段

        <com.projet.Core.Views.ProfileInfoItemView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          app:setNumber='@{account.posts.intValue()}'
          app:setTitle="Posts"/>

以上代码段不起作用。但如果我对其中的值进行硬编码,它的效果非常好。

attrs.xml

<declare-styleable name="ProfileInfoTextView">
    <attr name="setNumber" format="integer" />
    <attr name="setTitle" format="string" />
</declare-styleable>

来自ProfileInfoTextView类的片段

  private void initialize(Context context, AttributeSet attributeSet) {
    mRoot = inflate(context, R.layout.custom_profile_info_textview_container,this);
    mNumberTextView = (TextView) mRoot.findViewById(R.id.custom_profile_info_number_text_view);
    mTitleTextView = (TextView) mRoot.findViewById(R.id.custom_profile_info_title_text_view);

    if(attributeSet != null) {
      TypedArray a = context.getTheme().obtainStyledAttributes(
              attributeSet,
              R.styleable.ProfileInfoTextView,
              0, 0);
      try {
        mTitleTextView.setText(a.getString(R.styleable.ProfileInfoTextView_setTitle));
        String number = getNumber(a.getInt(R.styleable.ProfileInfoTextView_setNumber,1000));
        mNumberTextView.setText(number);
      } finally {
        a.recycle();
      }
    }
  }

来自帐户类的代码段

public class Account {
    private Integer posts;
    public Integer getPosts() {
        return posts;
    }

    public void setPosts(Integer posts) {
        this.posts = posts;
    }
}

Account.class是自动生成的GreenDao数据类。

我试图传入的值是Integer或int。两者都不起作用。只有当我在xml文件中对其进行硬编码时。 异常消息

Caused by: java.lang.RuntimeException: Found data binding errors.
****/ data binding error ****msg:Cannot find the setter for attribute 'app:setNumber' with parameter type java.lang.String on com.projet.Core.Views.ProfileInfoItemView. file:/Users/jemilriahi/ProjectNinjo/app/src/main/res/layout/fragment_my_account.xml loc:81:35 - 81:63 ****\ data binding error ****

    at android.databinding.tool.processing.Scope.assertNoError(Scope.java:110)
    at android.databinding.annotationprocessor.ProcessDataBinding.process(ProcessDataBinding.java:76)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcessor(JavacProcessingEnvironment.java:794)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment.discoverAndRunProcs(JavacProcessingEnvironment.java:705)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment.access$1800(JavacProcessingEnvironment.java:91)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment$Round.run(JavacProcessingEnvironment.java:1035)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(JavacProcessingEnvironment.java:1176)
    at com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:1170)
    at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:856)
    at com.sun.tools.javac.main.Main.compile(Main.java:523)

有人知道它不工作的原因吗?感谢所有帮助

1 个答案:

答案 0 :(得分:3)

好像您的ProfileInfoItemView课程没有setNumber方法可以获得String。在您的Java代码中,您在setNumber上调用的mNumberTextView看起来像一个孩子TextView,但数据绑定无法访问它。

您可以通过在setNumber中创建ProfileInfoItemView方法并在那里调用mNumberTextView.setText来解决此问题。