Is it possible to use Android tape data?

One of the coolest features of Android data binding support is that it also creates presentation fields with set identifiers. This simplifies the codebase since no fields or calls to findViewById() are needed.

But the problem is that a binding instance can only be obtained through a call to bind() , which tends to schedule the binding. This is bad when data is received asynchronously and a NullPointerException usually NullPointerException .

Is it possible to get a binding instance with View fields minus the actual data binding process?

Stack trace:

 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) 
+5
source share
2 answers

This, apparently, does not make sense, data binding ignores null variables, so the null pointer should not be thrown, that is, I consider it to be one of its most advanced functions. If you need to change variables after asynchronous calls, etc., you can simply use dataBinding.executePendingBindings()

From documents

The generated binding class will have a setter and getter for each of the described variables. Variables will take default Java values ​​until the setter is called - null for reference types, 0 for int, false for boolean, etc.

and

The generated data binding code automatically checks for zeros and throws null pointer exceptions. For example, in the expression @ {user.name}, if the user is null, the user is assigned the default value (null). If you referred to user.age, where age is int, then the default will be 0.

+3
source

Got the same problem with java.lang.Boolean . Solved using a primitive boolean type.

 <variable name="var" type="boolean" /> 
0
source

Source: https://habr.com/ru/post/1244473/


All Articles