Fixed
Status Update
Comments
yb...@google.com <yb...@google.com> #2
thanks for the sample.
Meanwhile, adding a safeUnbox to the first one should workaround the issue
Meanwhile, adding a safeUnbox to the first one should workaround the issue
yb...@google.com <yb...@google.com> #3
This is actually an interesting problem.
Debugging the issue, the state we are hitting is this:
DataBinding converts && into a ternary operation like this:
a && b = a ? b : false
Then in the code above, it reflects the following values:
java.lang.Boolean viewModelFlag1ViewModelFlag2BooleanFalse = null;
boolean androidxDatabindingViewDataBindingSafeUnboxViewModelFlag1GetValue = false;
java.lang.Boolean viewModelFlag2GetValue = null;
viewModelFlag1ViewModelFlag2BooleanFalse = ((androidxDatabindingViewDataBindingSafeUnboxViewModelFlag1GetValue) ? (viewModelFlag2GetValue) : (false));
// System.out.println(viewModelFlag1ViewModelFlag2BooleanFalse);
This works fine as itself, both on android or JVM but somehow fails in the data binding generated code.
Maybe I'm missing something trivial but debugger seems to agree as well :/.
Need to look at the byte code to really understand what is going on :/.
Debugging the issue, the state we are hitting is this:
DataBinding converts && into a ternary operation like this:
a && b = a ? b : false
Then in the code above, it reflects the following values:
java.lang.Boolean viewModelFlag1ViewModelFlag2BooleanFalse = null;
boolean androidxDatabindingViewDataBindingSafeUnboxViewModelFlag1GetValue = false;
java.lang.Boolean viewModelFlag2GetValue = null;
viewModelFlag1ViewModelFlag2BooleanFalse = ((androidxDatabindingViewDataBindingSafeUnboxViewModelFlag1GetValue) ? (viewModelFlag2GetValue) : (false));
// System.out.println(viewModelFlag1ViewModelFlag2BooleanFalse);
This works fine as itself, both on android or JVM but somehow fails in the data binding generated code.
Maybe I'm missing something trivial but debugger seems to agree as well :/.
Need to look at the byte code to really understand what is going on :/.
Description
lifecycle-extensions:2.1.0
Below code occurs NullPointerException in DataBinding.
xml
```
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:visible="@{viewModel.flag1 && viewModel.flag2}" />
```
ViewModel
```
class MainViewModel : ViewModel() {
val flag1 = MutableLiveData<Boolean>(true)
val flag2 = MutableLiveData<Boolean>()
}
```
Crash log
```
2019-11-11 17:11:49.453 13518-13518/com.example.sample E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.sample, PID: 13518
java.lang.RuntimeException: Failed to call observer method
at androidx.lifecycle.ClassesInfoCache$MethodReference.invokeCallback(ClassesInfoCache.java:226)
at androidx.lifecycle.ClassesInfoCache$CallbackInfo.invokeMethodsForEvent(ClassesInfoCache.java:194)
at androidx.lifecycle.ClassesInfoCache$CallbackInfo.invokeCallbacks(ClassesInfoCache.java:185)
at androidx.lifecycle.ReflectiveGenericLifecycleObserver.onStateChanged(ReflectiveGenericLifecycleObserver.java:36)
at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:361)
at androidx.lifecycle.LifecycleRegistry.forwardPass(LifecycleRegistry.java:300)
at androidx.lifecycle.LifecycleRegistry.sync(LifecycleRegistry.java:339)
at androidx.lifecycle.LifecycleRegistry.moveToState(LifecycleRegistry.java:145)
at androidx.lifecycle.LifecycleRegistry.handleLifecycleEvent(LifecycleRegistry.java:131)
at androidx.lifecycle.ReportFragment.dispatch(ReportFragment.java:123)
at androidx.lifecycle.ReportFragment.onStart(ReportFragment.java:83)
at android.app.Fragment.performStart(Fragment.java:2548)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1334)
at android.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1576)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1637)
at android.app.FragmentManagerImpl.dispatchMoveToState(FragmentManager.java:3046)
at android.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:3003)
at android.app.FragmentController.dispatchStart(FragmentController.java:193)
at android.app.Activity.performStart(Activity.java:7173)
at android.app.ActivityThread.handleStartActivity(ActivityThread.java:2975)
at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:180)
at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:165)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:142)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1816)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6718)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Boolean.booleanValue()' on a null object reference
at com.example.sample.databinding.ActivityMainBindingImpl.executeBindings(ActivityMainBindingImpl.java:175)
at androidx.databinding.ViewDataBinding.executeBindingsInternal(ViewDataBinding.java:473)
at androidx.databinding.ViewDataBinding.executePendingBindings(ViewDataBinding.java:445)
at androidx.databinding.ViewDataBinding$OnStartListener.onStart(ViewDataBinding.java:1687)
at java.lang.reflect.Method.invoke(Native Method)
at androidx.lifecycle.ClassesInfoCache$MethodReference.invokeCallback(ClassesInfoCache.java:216)
at androidx.lifecycle.ClassesInfoCache$CallbackInfo.invokeMethodsForEvent(ClassesInfoCache.java:194)
at androidx.lifecycle.ClassesInfoCache$CallbackInfo.invokeCallbacks(ClassesInfoCache.java:185)
at androidx.lifecycle.ReflectiveGenericLifecycleObserver.onStateChanged(ReflectiveGenericLifecycleObserver.java:36)
at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:361)
at androidx.lifecycle.LifecycleRegistry.forwardPass(LifecycleRegistry.java:300)
at androidx.lifecycle.LifecycleRegistry.sync(LifecycleRegistry.java:339)
at androidx.lifecycle.LifecycleRegistry.moveToState(LifecycleRegistry.java:145)
at androidx.lifecycle.LifecycleRegistry.handleLifecycleEvent(LifecycleRegistry.java:131)
at androidx.lifecycle.ReportFragment.dispatch(ReportFragment.java:123)
at androidx.lifecycle.ReportFragment.onStart(ReportFragment.java:83)
at android.app.Fragment.performStart(Fragment.java:2548)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1334)
at android.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1576)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1637)
at android.app.FragmentManagerImpl.dispatchMoveToState(FragmentManager.java:3046)
at android.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:3003)
at android.app.FragmentController.dispatchStart(FragmentController.java:193)
at android.app.Activity.performStart(Activity.java:7173)
at android.app.ActivityThread.handleStartActivity(ActivityThread.java:2975)
at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:180)
at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:165)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:142)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1816)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6718)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
```
It seems second LiveData is not working with safeUnbox.
I will attach sample project. Please check it.