为什么对空对象引用进行SavedStateHandle.set(java.lang.String,java.lang.Object)

时间:2019-09-13 15:10:11

标签: android android-jetpack

我使用viewmodel将片段绑定到viewmodel,并使用savestate,这是我的代码:

 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;
    }

handle显示为空引用,不能使用handle.set()函数,会发生什么情况?

0 个答案:

没有答案