我正在使用hilt并试图将我的导入SessionManager到我的AuthViewModel班级中。
当查看谷歌关于hilt with ViewModel的文档时,我们可以看到它们能够注入ExampleRepository repository到所述的构造函数中ExampleViewModel
public class ExampleViewModel extends ViewModel {
private final ExampleRepository repository;
private final SavedStateHandle savedStateHandle;
@ViewModelInject
ExampleViewModel(
ExampleRepository repository,
@Assisted SavedStateHandle savedStateHandle)
{
this.repository = repository;
this.savedStateHandle = savedStateHandle;
}
...
}
Run Code Online (Sandbox Code Playgroud)
和
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
exampleViewModel = new ViewModelProvider(this).get(ExampleViewModel.class);
}
Run Code Online (Sandbox Code Playgroud)
每当我尝试对我的项目执行此操作时,我都会不断收到错误消息。
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.atchat.donation, PID: 11005
java.lang.RuntimeException: Cannot create an instance of class com.atchat.donation.ui.auth.AuthViewModel
at …Run Code Online (Sandbox Code Playgroud)