Ссылка на зависимости Dagger2 для applicationComponent не предоставляет объект для внедрения - PullRequest
0 голосов
/ 06 июня 2018

Я следовал нескольким примерам для работы над 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;
 }

ShikshyaApplicationComponent:

  @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 вводится в

1 Ответ

0 голосов
/ 06 июня 2018

Вы должны включить ContextModule как HomeFragmentModule зависимость, чтобы Dagger2 мог предоставить от context до DatabaseHelper

@Module(includes = ContextModule.class)
public class HomeFragmentModule {
    // Your stuff here
}
...