问题
i use viewmodel to bind fragment to viewmodel,and use savestate,this is my code :
MyViewModel myViewModel = ViewModelProviders.of(getActivity(), new SavedStateViewModelFactory(getActivity().getApplication(),this)).get(MyViewModel.class);
FragmentTaskBinding taskBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_task, container, false);
taskBinding.setData(myViewModel);
taskBinding.setLifecycleOwner(getActivity());
private SavedStateHandle handle;
public MyViewModel(@NonNull Application application, SavedStateHandle handle) {
super(application);
if (!handle.contains("high")) {
SharedPreferences sharedPreferences = application.getSharedPreferences("high", Context.MODE_PRIVATE);
this.handle.set("high", sharedPreferences.getInt("high", 0));
this.handle.set("a", 0);
this.handle.set("b", 0);
this.handle.set("o", "+");
this.handle.set("s", 0);
}
this.handle = handle;
}
and handle was shown it's a null reference,can't use handle.set() function ,what happen?
来源:https://stackoverflow.com/questions/57926170/why-savedstatehandle-setjava-lang-string-java-lang-object-on-a-null-object-re