【问题标题】:databinding cannot find method数据绑定找不到方法
【发布时间】: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


    【解决方案1】:

    是的,因为 Textview android:text 只接受字符串值:

    android:text="@{String.format(@string/start_weight, DateUtil.getMediumStringFromMilli(objective.startTime))}"
    

    【讨论】:

      猜你喜欢
      • 2016-08-01
      • 2013-11-25
      • 1970-01-01
      • 2018-06-25
      • 1970-01-01
      • 1970-01-01
      • 2016-10-12
      • 2021-12-31
      • 2021-09-07
      相关资源
      最近更新 更多