双向绑定无法解析java.lang.String属性的setter [英] Two-way binding cannot resolve a setter for java.lang.String property

查看:751
本文介绍了双向绑定无法解析java.lang.String属性的setter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Android Studio 2.1 AFIK中引入的数据绑定API的双向绑定.

I am playing with the two-way binding of the data binding API which was introduced in Android Studio 2.1 AFIK.

我得到一个有趣的错误:

I get this interesting error:

错误:任务':app:compileDebugJavaWithJavac'的执行失败.
> java.lang.RuntimeException:发现数据绑定错误.
****/数据绑定错误**** msg:无法反转表达式address.street:双向绑定无法解析java.lang.String属性'street'的setter
文件:/path/to/layout.xml
位置:34:37-34:50 **** \数据绑定错误****

Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> java.lang.RuntimeException: Found data binding errors.
****/ data binding error ****msg:The expression address.street cannot cannot be inverted: Two-way binding cannot resolve a setter for java.lang.String property 'street'
file:/path/to/layout.xml
loc:34:37 - 34:50 ****\ data binding error ****

当我尝试搜索该错误时,我只是从一个哭泣的家伙那里找到了一个4天大的日语Twitter帖子...

When I try to google that error I just find a 4 day old Japanese Twitter posting from a guy who is crying about it...

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/edit_hint_zip"
    android:text="@={address.zip}"
    tools:text="12345"/>

address.zipString.我猜这里的问题是CharSequence相对于String作为EditText.getText()的返回值.

That address.zip is a String. I am guessing that the problem here is CharSequence vs. String as the return value of EditText.getText().

我的想法是对其进行定义,但这对我不起作用:

My idea was to defining it however this does not work for me:

@NonNull
@InverseBindingAdapter(attribute = "text")
public static String getText(EditText edit) {
    return edit.getText().toString();
}

我想念什么?

推荐答案

此错误很糟糕,它也是数据绑定API中的错误.解决方案是生成一个setter 和一个 getter .我很快想到了创建setter的想法,而不是创建getter的想法.

This bug is ugly as hell and properly a bug in the data binding API. The solution is to generate a setter and a getter. I came up fast with the idea to create a setter, but not to create a getter.

现在是我的简化模型:

public class Address {
    public String street;

    public void setStreet(String street) {
        this.street = street;
    }

    public String getStreet() {
        return street;
    }
}

您可能会注意到,getter和setter没用,但是双向绑定是必需的.

As you may note the getter and setter are useless, but required for two way binding.

如果您认为这是API的错误,请给我的错误报告加注星标:

If you think that this is a bug of the API please star my bug report: Two-way binding required setters AND ALSO getters

这篇关于双向绑定无法解析java.lang.String属性的setter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆