【发布时间】:2020-01-25 19:08:54
【问题描述】:
我正在尝试将CacheManager 的实例注入GetTodoRepository,Dagger 可能会成功执行此操作,因为我没有收到任何与匕首相关的错误。但是当在GetTodoRepository 中使用cacheManager 时,我得到一个 NullPointerException
public class GetTodoRepository {
@Inject
public CacheManager cacheManager;
public RetrofitService retrofitService;
private ResultListener listener;
public GetTodoRepository(@NonNull ResultListener listener) {
this.retrofitService = new RetrofitService();
this.listener = listener;
}
}
@Module
public class AppModule {
private Application application;
public AppModule(Application application) {
this.application = application;
}
@Provides
@Singleton
public Context providesApplicationContext() {
return application.getApplicationContext();
}
@Provides
@Singleton
public CacheManager provideCacheManager(Context Context) {
return new CacheManager(Context);
}
}
@Singleton
@Component(modules = AppModule.class)
public interface TodoComponents {
void inject(MainViewModel mainViewModel);
void inject(CacheManager cacheManager);
void inject(GetTodoRepository getTodoRepository);
void inject(PostTodoRepository postTodoRepository);
}
【问题讨论】:
-
你的组件是什么样的?
-
@Md.Asaduzzaman 组件类现在在问题中可见
-
你必须 注入
GetTodoRepository才能匕首。你这样做了吗? -
@Md.Asaduzzaman 我只做了我粘贴的内容。注入匕首是什么意思?
标签: java android dagger-2 dagger