我正在使用Android应用程序,目的是尝试数据绑定,然后研究我发现了一条与此类似的行(我写的那个):
@{@string/dateFormat(person.birthday.year, person.birthday.month + person.birthday.day)}
字符串资源是:
<string name="dateFormat">%2$d/%3$d/%1$d</string>
在示例中说它应该可以工作,但我一直收到此错误:
No resource found that matches the given name (at \u0027text\u0027 with value \u0027@{@string/dateFormat(person.birthday.year,person.birthday.month+1,person.birthday.day)\u0027)
绑定的xml是:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{@string/dateFormat(user.birthday.year, user.birthday.month + 1, user.birthday.day)}"/>
人物模型是:
public class Person implements Serializable{
private String firstName;
private String lastName;
private String phoneNumber;
private PickedDate birthday;
// Irrelevant Code Omitted
PickedDate在哪里:
public class PickedDate {
private final ObservableInt year;
private final ObservableInt month;
private final ObservableInt day;
正如我所看到的那样,似乎将引号误认为是unicode值。
我研究过但无法找到任何其他类似装订的地方。
提前致谢
答案 0 :(得分:0)
我已经复制了您的布局和模型,并尝试将其放入hello world项目中。它可以在没有
的情况下成功编译public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding binding = DataBindingUtil.inflate(LayoutInflater.from(this),R.layout.activity_main,null,false);
Person lUser = new Person();
Person.PickedDate lBirthday = new Person.PickedDate();
lBirthday.getDay().set(3);
lBirthday.getMonth().set(4);
lBirthday.getYear().set(5);
lUser.setBirthday(lBirthday);
binding.setUser(lUser);
setContentView(binding.getRoot());
}
}
模型类
public class Person implements Serializable {
private String firstName;
private String lastName;
private String phoneNumber;
private PickedDate birthday;
public String getFirstName() {
return firstName;
}
public void setFirstName(String pFirstName) {
firstName = pFirstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String pLastName) {
lastName = pLastName;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String pPhoneNumber) {
phoneNumber = pPhoneNumber;
}
public PickedDate getBirthday() {
return birthday;
}
public void setBirthday(PickedDate pBirthday) {
birthday = pBirthday;
}
public static class PickedDate {
private final ObservableInt year;
private final ObservableInt month;
private final ObservableInt day;
public PickedDate() {
year = new ObservableInt();
month = new ObservableInt();
day = new ObservableInt();
}
public ObservableInt getYear() {
return year;
}
public ObservableInt getMonth() {
return month;
}
public ObservableInt getDay() {
return day;
}
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="user"
type=".Person"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{@string/dateFormat(user.birthday.year, user.birthday.month + 1, user.birthday.day)}"/>
</LinearLayout>
</layout>
的build.gradle
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "example.com.testerapplication"
minSdkVersion 8
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
dataBinding {
enabled = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:cardview-v7:23.4.0'
compile 'com.android.support:design:23.4.0'
}