Dagger 注入 class 在 Dagger 中一直为空
Dagger injected class keeps being null with Dagger
我正在尝试将 CacheManager
的一个实例注入 GetTodoRepository
中,Dagger 可能会成功,因为我没有收到任何与 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);
}
1) 在应用程序中设置 Dagger(组件)class
public class TodoApplication extends Application {
private static AppComponent components;
@Override
public void onCreate() {
super.onCreate();
components = DaggerAppComponent.builder()
.appModule(new AppModule(this))
.build();
}
public static AppComponent getAppComponent() {
return components;
}
}
2) 在注入目标class
中调用来自Application
class的Component
对象
public GetTodoRepository(@NonNull ResultListener listener) {
this.listener = listener;
TodoApplication.getAppComponent().inject(this);
}
我正在尝试将 CacheManager
的一个实例注入 GetTodoRepository
中,Dagger 可能会成功,因为我没有收到任何与 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);
}
1) 在应用程序中设置 Dagger(组件)class
public class TodoApplication extends Application {
private static AppComponent components;
@Override
public void onCreate() {
super.onCreate();
components = DaggerAppComponent.builder()
.appModule(new AppModule(this))
.build();
}
public static AppComponent getAppComponent() {
return components;
}
}
2) 在注入目标class
中调用来自Application
class的Component
对象
public GetTodoRepository(@NonNull ResultListener listener) {
this.listener = listener;
TodoApplication.getAppComponent().inject(this);
}