【发布时间】:2020-07-13 01:25:59
【问题描述】:
我开始在 android 中使用数据绑定,但遇到了问题。尝试构建项目时,出现错误
发现数据绑定错误: [数据绑定] {"msg":"在 com.sgc.weightcontrol.util.DateUtil 类中找不到方法 getMediumStringFromMilli(float)","file":"B:\projects\weightControl\app\src\main\res\layout \fragment_objective.xml","pos":[{"line0":49,"col0":58,"line1":49,"col1":115}]}
我的片段 xml (R.layout.fragment_objective)
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<import type="com.sgc.weightcontrol.util.DateUtil" />
<variable
name="objective"
type="com.sgc.weightcontrol.ui.modelUI.ObjectiveUI" />
</data>
//Container open
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{`string/start_weight` + DateUtil.getMediumStringFromMilli(objective.startTime)}" " />
//Container close
</layout>
日期实用程序
public class DateUtil {
private static DateTimeFormatter dateFormatMedium =
DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).withLocale(Locale.getDefault());
public static String getMediumStringFromMilli(long milli){
LocalDateTime time = Instant.ofEpochMilli(milli).atZone(ZoneId.systemDefault()).toLocalDateTime();
return dateFormatMedium.format(time);
}
}
build.gradle
dependencies {
classpath 'com.android.tools.build:gradle:3.6.1'
classpath 'com.android.databinding:dataBinder:1.0-rc0'
}
build.gradle(模块)
dataBinding {
enabled = true
}
获取绑定
FragmentObjectiveBinding binding = DataBindingUtil.setContentView(getActivity(), R.layout.fragment_objective);
我的 android-studio 版本 = 3.6.2
我尝试清除缓存/重启,更改片段xml文件名。
【问题讨论】:
标签: java android android-fragments gradle data-binding