Android Dagger2对applicationComponent的依赖项引用未提供要注入的对象
我按照一些例子来研究dagger2。在这里,我使用HomeFragmentComponent上的依赖项从另一个范围提供上下文引用,但它不起作用 上下文模块Android Dagger2对applicationComponent的依赖项引用未提供要注入的对象,android,dagger-2,dagger,Android,Dagger 2,Dagger,我按照一些例子来研究dagger2。在这里,我使用HomeFragmentComponent上的依赖项从另一个范围提供上下文引用,但它不起作用 上下文模块 @Module public class ContextModule { private final Context context; public ContextModule(Context context) { this.context = contex
@Module
public class ContextModule {
private final Context context;
public ContextModule(Context context) {
this.context = context;
}
@Provides
@ShikshyaScope
public Context context(){
return context;
}
}
网络模块:
@Module(includes = ContextModule.class)
public class NetworkModule {
@Provides
@ShikshyaScope
public File file(Context context){
File cacheFile = new File(context.getCacheDir(),"okhttp_cache");
cacheFile.mkdirs();
return cacheFile;
}
Shikshya应用程序组件:
@ShikshyaScope
@Component(modules = {NetworkModule.class, PicassoModule.class, StorageModule.class})
public interface ShikshyaApplicationComponent {
void injectShikshyaApplication(ShikshyaBusApplication shikshyaBusApplication);
}
主片段模块:
@Module
public class HomeFragmentModule {
public final HomeFragment homeFragment;
public HomeFragmentModule(HomeFragment homeFragment) {
this.homeFragment = homeFragment;
}
@Provides
@HomeFragmentScope
public HomeFragment homeFragment(){
return homeFragment;
}
@Provides
@HomeFragmentScope
public HomeFragmentView homeFragmentView(HomeFragment homeFragment){
return (HomeFragmentView)homeFragment;
}
@Provides
@HomeFragmentScope
public HomeFragmentPresenter homeFragmentPresenter(HomeFragmentView homeFragmentView,MetaDatabaseRepo metaDatabaseRepo){
return new HomeFragmentPresenter(homeFragmentView,metaDatabaseRepo);
}
@Provides
@HomeFragmentScope
public DatabaseHelper databaseHelper(Context context){
return OpenHelperManager.getHelper(context,DatabaseHelper.class);
}
}
HomeFragmentComponent:
@HomeFragmentScope
@Component(modules = HomeFragmentModule.class,dependencies =ShikshyaApplicationComponent.class)
public interface HomeFragmentComponent {
void injectHomeFragment(HomeFragment homeFragment);
}
现在我得到的错误是
error: android.content.Context cannot be provided without an @Provides-annotated method.
android.content.Context is injected at com.bihani.shikshyabus.di.module.HomeFragmentModule.databaseHelper(context)
com.bihani.shikshyabus.database.DatabaseHelper在处被注入,您应该将
ContextModule
作为homepragmentmodule
依赖项,以便Dagger2能够向DatabaseHelper
提供上下文
@Module(includes = ContextModule.class)
public class HomeFragmentModule {
// Your stuff here
}
如何向ContextModule提供上下文?@AlexTa ShikshyaApplicationComponent component=DaggerShikshyaApplicationComponent.builder().ContextModule(新ContextModule(this)).build();但在此之前,我无法构建该项目,因此我认为首先需要解决上述问题才能获得DaggerObject是的,我之前尝试过,然后它给了我以下错误:com.bihani.shikshyabus.ui.home.homeFragment.HomeFragmentComponent作用域为com.bihani.shikshyabus.di.scope.HomeFragmentScope可能没有引用具有不同作用域的绑定:提供com.bihani.shikshyabus.di.scope.shikshyabose android.content.Context com.bihani.shikshyabus.di.module.ContextModule.Context()