简体   繁体   中英

Android: Why data binding not forcing NullPointerException even if object is null

I am not sure why data binding not allowing crash even if the object is null, Some time it's not okay to avoid null pointer exceptions, as a result user might see empty data or get confused by seeing empty TextFild.

Eg:

android:text="@{user.lastName}"

If above user object is null, then we will see empty textView, where user can think they don't have the last name in database/network server.

Is there any parameter where we can force NullPointerException if the object is null?

您可以在var的getter方法中检查nullPointerException以获取变量,甚至可以像波纹管这样的数据绑定集文本中设置null条件

android:text="@{user.lastName != null ? user.lastName : "unknown text" }"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM