Я следовал нескольким примерам для работы над dagger2. Здесь я использовал зависимости от HomeFragmentComponent, чтобы предоставить ссылку на контекст из другой области, но он не работал.
ContextModule
@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;
}
Компонент приложения:
@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 вводится в
@AlexTa ShikshyaApplicationComponent component = DaggerShikshyaApplicationComponent.builder () .contextModule (новый ContextModule (это)) .build (); Но до этого я не смог собрать проект, поэтому я думаю, что сначала необходимо решить указанную выше проблему, чтобы получить DaggerObject.
Вы должны включить ContextModule как зависимость HomeFragmentModule, чтобы Dagger2 мог предоставить context в DatabaseHelper.
@Module(includes = ContextModule.class)
public class HomeFragmentModule {
// Your stuff here
}
Да, я пробовал это раньше, тогда это дает мне следующую ошибку: com.bihani.shikshyabus.ui.home.homeFragment.HomeFragmentComp onent scoped with com.bihani.shikshyabus.di.scope.HomeFragmentScope может не ссылаться на привязки с разными областями: Предоставляет com.bihani.shikshyabus.di.scope.ShikshyaScope android.content.Context com.bihani.shikshyabus.di.module.ContextModule.context () '
Как вы предоставляете контекст для ContextModule?