Jemil Riahi
Jemil Riahi

Reputation: 1370

Databinding error with attributes for view

Maybe someone here could explain to me why this is not working and how i could solve it.

Snippet from xml View file

        <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"/>

The above snippet does not work. But it works perfectly if i hardcode the value in.

attrs.xml

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

Snippet from ProfileInfoTextView class

  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();
      }
    }
  }

Snippet from account class

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

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

The Account.class is autogenerated GreenDao data class.

The value i am trying to pass in is either a Integer or a int. Both does not work. Only when i hardcode it in the xml file. Exception message

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)

Anyone know the reason why it is not working? All help is appreciated

Upvotes: 3

Views: 3454

Answers (1)

yigit
yigit

Reputation: 38243

Seems like your ProfileInfoItemView class does not have a setNumber method that receive String. In your Java code, you are calling setNumber on mNumberTextView which looks like a child TextView but data binding has no way of accessing it.

You can solve this by creating a setNumber method in ProfileInfoItemView and call mNumberTextView.setText there.

Upvotes: 5

Related Questions