我试图将ToDoRepository注入我的ToDoDeleteNotificationService,并始终将此存储库设为null。我不知道我是否需要更多地执行另一个模块,或者我是否可以在IntentService中执行此操作。
@Module
public class ToDoRepositoryModule {
@Provides
@Singleton
ToDoDatabase provideDatabase(Application application) {
return Room.databaseBuilder(application,
ToDoDatabase.class, "ToDo.db")
.build();
}
@Provides
@Singleton
ToDoDao provideUserDao(ToDoDatabase database) {
return database.toDoDao();
}
@Provides
@Singleton
AppExecutors provideAppExecutors() {
return new AppExecutors();
}
@Provides
@Singleton
ToDoRepository providesToDoRepository(ToDoDao toDoDao, AppExecutors appExecutors) {
return new ToDoRepository(toDoDao, appExecutors);
}
}
- 当调用我的类TodoDeleteNotiricationService时,我的todoId byt我的存储库为null
public class ToDoDeleteNotificationService extends IntentService {
@Inject
ToDoRepository mToDoRepository;
public ToDoDeleteNotificationService() {
super("ToDoDeleteNotificationService");
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
assert intent != null;
String toDoId = intent.getStringExtra(ToDoNotificationsService.TODO_MODEL_KEY);
mToDoRepository.deleteToDo(toDoId);
}
}
-
@Singleton
@Component(modules = {ToDoRepositoryModule.class,
ActivityBindingModule.class,
AppModule.class,
AndroidSupportInjectionModule.class})
public interface AppComponent extends AndroidInjector<ToDoApplication>{
ToDoRepository getToDoRepository();
@Component.Builder
interface Builder{
@BindsInstance
AppComponent.Builder application(Application application);
AppComponent build();
}
}