出现这种情况,要去ViewModelProvider类---> get方法去打断点,看看ViewModel viewModel = mViewModelStore.get(key)这条语句的返回值,是否成功查出已有的viewModel,如果没查出的话会走create方法,创建一个新的viewModel,所以就为null了。
/**
* Returns an existing ViewModel or creates a new one in the scope (usually, a fragment or
* an activity), associated with this {@code ViewModelProvider}.
* <p>
* The created ViewModel is associated with the given scope and will be retained
* as long as the scope is alive (e.g. if it is an activity, until it is
* finished or process is killed).
*
* @param key The key to use to identify the ViewModel.
* @param modelClass The class of the ViewModel to create an instance of it if it is not
* present.
* @param <T> The type parameter for the ViewModel.
* @return A ViewModel that is an instance of the given type {@c