无法在 xml 中创建当前日期:输入不匹配 'Date'
Can't create current date in xml: mismatched input 'Date'
Android Studio 3.0。
我使用数据绑定。
这是我的 xml:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<import type="java.util.Date" />
<import type="com.myproject.util.date.DateUtil" />
<variable
name="item"
type="com.myproject.model.Purchase" />
</data>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/validStillValueTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_marginTop="10dp"
android:textColor='@{DateUtil.getDiffDays(item.product.dateUntil, item.product.dateUntil) == 0 ? @color/strikethrough : @color/textColorPrimary}'
android:textSize="9sp"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="@+id/validStillLabelTextView"
app:layout_constraintTop_toBottomOf="@+id/separator2" />
</android.support.constraint.ConstraintLayout>
</layout>
这里方法:
public static long getDiffDays(Date dateFrom, Date dateTo) {
long diff = getDiffMilliseconds(dateFrom, dateTo);
return diff / (24 * 60 * 60 * 1000);
}
结果它工作正常。不错
但现在我想将当前日期(按 new Date()
)作为 xml 文件中的第二个参数传递。
因此 xml 的变化:
<TextView
android:id="@+id/validStillValueTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_marginTop="10dp"
android:textColor='@{DateUtil.getDiffDays(item.product.dateUntil, new Date()) == 0 ? @color/strikethrough : @color/textColorPrimary}'
android:textSize="9sp"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="@+id/validStillLabelTextView"
app:layout_constraintTop_toBottomOf="@+id/separator2" />
但现在我得到错误:
* What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
> android.databinding.tool.util.LoggedErrorException: Found data binding errors.
****/ data binding error ****msg:Syntax error: mismatched input 'Date' expecting {',', ')', '.', '::', '[', '+', '-', '*', '/', '%', '<<', '>>>', '>>', '<=', '>=', '>', '<', 'instanceof', '==', '!=', '&', '^', '|', '&&', '||', '?', '??'}
file:myproject\app\src\main\res\layout\purchase_details.xml
loc:207:49 - 207:158
****\ data binding error ****
数据绑定表达式中不允许关键字 new
。如果一定要访问一个新的对象,就必须使用一个方法来生成它。
Android Studio 3.0。 我使用数据绑定。
这是我的 xml:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<import type="java.util.Date" />
<import type="com.myproject.util.date.DateUtil" />
<variable
name="item"
type="com.myproject.model.Purchase" />
</data>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/validStillValueTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_marginTop="10dp"
android:textColor='@{DateUtil.getDiffDays(item.product.dateUntil, item.product.dateUntil) == 0 ? @color/strikethrough : @color/textColorPrimary}'
android:textSize="9sp"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="@+id/validStillLabelTextView"
app:layout_constraintTop_toBottomOf="@+id/separator2" />
</android.support.constraint.ConstraintLayout>
</layout>
这里方法:
public static long getDiffDays(Date dateFrom, Date dateTo) {
long diff = getDiffMilliseconds(dateFrom, dateTo);
return diff / (24 * 60 * 60 * 1000);
}
结果它工作正常。不错
但现在我想将当前日期(按 new Date()
)作为 xml 文件中的第二个参数传递。
因此 xml 的变化:
<TextView
android:id="@+id/validStillValueTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_marginTop="10dp"
android:textColor='@{DateUtil.getDiffDays(item.product.dateUntil, new Date()) == 0 ? @color/strikethrough : @color/textColorPrimary}'
android:textSize="9sp"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="@+id/validStillLabelTextView"
app:layout_constraintTop_toBottomOf="@+id/separator2" />
但现在我得到错误:
* What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
> android.databinding.tool.util.LoggedErrorException: Found data binding errors.
****/ data binding error ****msg:Syntax error: mismatched input 'Date' expecting {',', ')', '.', '::', '[', '+', '-', '*', '/', '%', '<<', '>>>', '>>', '<=', '>=', '>', '<', 'instanceof', '==', '!=', '&', '^', '|', '&&', '||', '?', '??'}
file:myproject\app\src\main\res\layout\purchase_details.xml
loc:207:49 - 207:158
****\ data binding error ****
数据绑定表达式中不允许关键字 new
。如果一定要访问一个新的对象,就必须使用一个方法来生成它。