初始化SavedStateViewModelFactory SavedStateRegistryOwner运行报错

implementation 'androidx.lifecycle:lifecycle-viewmodel-savedstate:2.3.0-alpha03'
使用这个版本的SavedStateViewModelFactory 按照Jetpack官方文档去初始化:
    

MyViewModel myViewModel = new ViewModelProvider(this,new SavedStateViewModelFactory(getApplication(),this)).get(MyViewModel.class);


需要在当前Activity实现SavedStateRegistryOwner方法,
    

    @NonNull
    @Override
    public SavedStateRegistry getSavedStateRegistry() {
        return null;
    }


直接返回null,运行会报错:Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 
'android.os.Bundle androidx.savedstate.SavedStateRegistry.consumeRestoredStateForKey(java.lang.String)' on a null object reference
。    

解决办法:升级当前 implementation 'androidx.appcompat:appcompat:1.0.0'

implementation 'androidx.appcompat:appcompat:1.1.0' 或以上

你可能感兴趣的:(安卓技术心得)