Android Integer 类型的双向绑定导致数据绑定不存在 [英] Android two way binding with Integer type causes databinding does not exist

查看:44
本文介绍了Android Integer 类型的双向绑定导致数据绑定不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用整数数据类型实现双向绑定时遇到了一些问题.

I'm having some issue with implementing two way binding with an Integer data type.

public class User {

    private String firstName;
    private String lastName;
    private int age;

    public User() {}

    public void setFirstName(String firstName) {
       this.firstName = firstName;
    }

    public String getFirstName() {
       return this.firstName;
    }

    public void setLastName(String lastName) {
       this.lastName = lastName;
    }

    public String getLastName() {
       return this.lastName;
    }

    public void setAge(int age) {
       this.age = age;
    }

    public int getAge() {
       return this.age;
    }

}

XML:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data class="UserDataBinding">
        <variable
            name="user"
            type="com.databinding.model.User" />
    </data>

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="@dimen/activity_horizontal_margin">

       <EditText android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@={user.firstName}" />

       <EditText android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@={user.lastName}" />

       <EditText android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@={user.age}" />

    </LinearLayout>
</layout>

不幸的是,它给了我错误

Unfortunately, it gives me the error

"错误:(52, 17) 找不到属性 'android:text' 的 getter值类型为 java.lang.Integerandroid.support.design.widget.TextInputEditText."

"Error:(52, 17) Cannot find the getter for attribute 'android:text' with value type java.lang.Integer on android.support.design.widget.TextInputEditText. "

如果我将属性文本更改为

If I change the attribute text to

       <EditText android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@={Integer.toString(user.age)}" />

然后我得到错误

错误:无法生成视图绑定器 java.lang.NullPointerException"

"Error:cannot generate view binders java.lang.NullPointerException"

感谢您对此的任何帮助.

Appreciate any help on this.

更新:在上面提到的错误之后似乎还有另一个错误.

UPDATE: It seems there was another error right after the error mentioned above.

无法生成视图绑定器 java.lang.NullPointerException

cannot generate view binders java.lang.NullPointerException

即使应用程序尚未启动,也不知道为什么它给了我 NPE.

Not sure why its giving me NPE even though the app hasn't started yet.

推荐答案

好吧,六个月后,但也许我可以帮助某人.

Well, six months later but maybe i can help someone.

你可以做这个简单的技巧:

You can do this simple trick:

android:text="@={`` + mObject.someNumber}"

OBS.:您至少需要 Android Studio 2.3

这篇关于Android Integer 类型的双向绑定导致数据绑定不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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