尝试使用 ViewModelProvider 时出现以下错误:
BeaconViewModel beaconViewModel = new ViewModelProvider(LoginActivity.this).get(BeaconViewModel.class);
“ViewModelProvider(androidx.lifecycle.viewmodel.ViewModelProviderImpl)”在“androidx.lifecycle.ViewModelProvider”中具有私有访问权限
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
implementation "androidx.room:room-rxjava2:$room_version"
implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-runtime:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:$lifecycle_version"
implementation 'androidx.lifecycle:lifecycle-common-java8:2.8.2'
implementation "androidx.lifecycle:lifecycle-service:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-process:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-reactivestreams:$lifecycle_version"
使用这些版本的依赖项对我有用:
def lifecycle_version = "2.2.0"
implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version"