我想我错过了一些东西。我收到此错误:
PostsVM cannot be provided without an @Inject constructor or from an
@Provides-annotated method.
假设类如下:
@Module
public class AppModule {
private final Application mApplication;
@Singleton
@Provides
ViewModel provideListViewModel() {
return new PostsVM();
}
}
还有一类PostVM
@Singleton
public class PostsVM extends ViewModel {
public boolean get(){
return true;
}
}
还有一个组件:
@Singleton
@Component(modules = AppModule.class)
public interface AppComponent {
void inject(Global global);
void inject(MainActivity mainActivity);
@Architecture.ApplicationContext
Context getContext();
Application getApplication();
}
在活动中:
@Inject
public ViewModelProvider.Factory factory;
@Override
protected void onCreate(Bundle savedInstanceState) {
InjectorClass.inject(this);
如您所见,为 PostVM 类提供的示例不依赖于任何东西,为什么我需要一个 @inject 构造函数?