I started working with databinding in android and I had a problem.When trying to build a project, an error occurs
Found data binding error(s): [databinding] {"msg":"cannot find method getMediumStringFromMilli(float) in class com.sgc.weightcontrol.util.DateUtil","file":"B:\projects\weightControl\app\src\main\res\layout\fragment_objective.xml","pos":[{"line0":49,"col0":58,"line1":49,"col1":115}]}
My fragment 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>
DateUtil
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(module)
dataBinding {
enabled = true
}
Getting binding
FragmentObjectiveBinding binding = DataBindingUtil.setContentView(getActivity(), R.layout.fragment_objective);
My android-studio version = 3.6.2
I tried to clear the cache / restart, change the fragment xml file name.
Yes Because Textview android:text accept only string value:
android:text="@{String.format(@string/start_weight, DateUtil.getMediumStringFromMilli(objective.startTime))}"