Android数据绑定支持最酷的功能之一是它还为View设置了ID字段.这样可以整理代码库,因为不需要字段或findViewById()调用.
但问题是绑定实例只能通过bind()调用来检索,该调用往往会调度绑定.当异步接收数据并且通常抛出NullPointerException时,这很糟糕.
可以检索带有View字段的绑定实例减去实际的数据绑定过程吗?
堆栈跟踪:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Boolean.booleanValue()' on a null object reference
at com.app.android.databinding.ActivityRestaurantDetailsBinding.executeBindings(ActivityRestaurantDetailsBinding.java:381)
at android.databinding.ViewDataBinding.executePendingBindings(ViewDataBinding.java:350)
at android.databinding.ViewDataBinding$6.run(ViewDataBinding.java:167)
at android.databinding.ViewDataBinding$5.onViewAttachedToWindow(ViewDataBinding.java:137)
at android.view.View.dispatchAttachedToWindow(View.java:14525)
从the docs起
The generated binding class will have a setter and getter for each of the described variables. The variables will take the default Java values until the setter is called — null for reference types,0 for int,false for boolean,etc.
和
Generated data binding code automatically checks for nulls and avoid null pointer exceptions. For example,in the expression @{user.name},if user is null,user.name will be assigned its default value (null). If you were referencing user.age,where age is an int,then it would default to 0.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。