【发布时间】:2016-09-06 06:42:35
【问题描述】:
当我使用我的StringUtils 类格式化日期时,我得到了一个NullPointerException。如果我在没有StringUtils 的情况下使用它,它就可以正常工作。
我已经为StringUtils添加了导入语句
我有这个:
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@{StringUtils.getFormattedDate(user.date)}" />
这使我的StringUtils 方法出现错误:
public static String getFormattedDate(String unformattedDate) {
// unformattedDate will be in format of yyyy-mm-dd
// Convert it to d mmm, yyyy
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String date = null;
try {
Date d = df.parse(unformattedDate); // <<--------- Here
SimpleDateFormat dateFormat = new SimpleDateFormat("d MMM, yyyy");
date = dateFormat.format(d.getTime());
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
当我用调试器检查时,unformattedDate 从一开始就是null。调用了正确的方法,但传递的值为null。这很奇怪。
当我在布局文件中这样使用它时:
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@{user.date}" />
它没有给我任何错误,并且日期显示在屏幕上!
我已尝试清理项目并重新运行它。但没有成功。
【问题讨论】:
-
您在何时何地为
user设置值? -
它是使用 firebase 数据库异步获取的。这会导致问题吗?
标签: android data-binding android-databinding