无法将 textview 绑定到来自公开的不可实例化的 final class 的静态方法
Not able to bind textview to a static method from a publicly non instantiable final class
我想将我的一个实用程序 class 绑定到文本视图。我在数据字段中包含了我的 Utils class。当我 运行 应用程序出现错误,如未找到方法
我的xml文件
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import type="PACKAGE_NAME.utils.CommonUtils"/>
<variable
name="viewModel"
type="PACKAGE_NAME.PACKAGE_UI_PATH.RecordItemViewModel" />
</data>
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:elevation="5dp"
app:cardElevation="5dp"
tools:targetApi="lollipop">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14.5sp"
android:textStyle="normal"
android:textColor="#66000000"
android:id="@+id/dateTv"
android:lineSpacingExtra="4.5sp"
android:gravity="center_horizontal"
android:text="@{CommonUtils.returnDiffFromCurrentDate(viewModel.mDate)}
" tools:text="1 day ago"
android:layout_marginTop="15dp"
android:layout_marginRight="15dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
</layout>
我的实用程序文件是
public final class CommonUtils {
private CommonUtils() {
// This utility class is not publicly instantiable
}
public static String returnDiffFromCurrentDate(long givenTime)
{
long timeGap = System.currentTimeMillis() - givenTime;
return String.valueOf(TimeUnit.MILLISECONDS.toDays(timeGap));
}
}
我收到以下错误消息
发现数据绑定错误。
****/ 数据绑定错误 ****msg: 在 class PACKAGE_NAME.utils.CommonUtils
中找不到方法 returnDiffFromCurrentDate(java.lang.String)
错误说它找不到一个名为 returnDiffFromCurrentDate 的方法,只要它在你的 CommonUtils 中就没有了。class.
然后问题出在 viewModel.mDate 似乎是字符串,而不是长字符串
您应该使用任何其他方法将其转换为 long 或更改 returnDiffFromCurrentDate 以期待 String
我想将我的一个实用程序 class 绑定到文本视图。我在数据字段中包含了我的 Utils class。当我 运行 应用程序出现错误,如未找到方法
我的xml文件
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import type="PACKAGE_NAME.utils.CommonUtils"/>
<variable
name="viewModel"
type="PACKAGE_NAME.PACKAGE_UI_PATH.RecordItemViewModel" />
</data>
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:elevation="5dp"
app:cardElevation="5dp"
tools:targetApi="lollipop">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14.5sp"
android:textStyle="normal"
android:textColor="#66000000"
android:id="@+id/dateTv"
android:lineSpacingExtra="4.5sp"
android:gravity="center_horizontal"
android:text="@{CommonUtils.returnDiffFromCurrentDate(viewModel.mDate)}
" tools:text="1 day ago"
android:layout_marginTop="15dp"
android:layout_marginRight="15dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
</layout>
我的实用程序文件是
public final class CommonUtils {
private CommonUtils() {
// This utility class is not publicly instantiable
}
public static String returnDiffFromCurrentDate(long givenTime)
{
long timeGap = System.currentTimeMillis() - givenTime;
return String.valueOf(TimeUnit.MILLISECONDS.toDays(timeGap));
}
}
我收到以下错误消息
发现数据绑定错误。 ****/ 数据绑定错误 ****msg: 在 class PACKAGE_NAME.utils.CommonUtils
中找不到方法 returnDiffFromCurrentDate(java.lang.String)错误说它找不到一个名为 returnDiffFromCurrentDate 的方法,只要它在你的 CommonUtils 中就没有了。class.
然后问题出在 viewModel.mDate 似乎是字符串,而不是长字符串
您应该使用任何其他方法将其转换为 long 或更改 returnDiffFromCurrentDate 以期待 String