简体   繁体   English

如果没有@ Provide-annotated方法,则无法提供Dagger 2

[英]Dagger 2 cannot be provided without an @Provides-annotated method

I know there are many similar questions but I still haven't been able to find a solution for my problem and at this stage, I'm out of ideas. 我知道有很多类似的问题,但我仍然无法找到解决问题的方法,而且在这个阶段,我没有想法。 I have the following setup: 我有以下设置:

  • Application module/component: just for the context and the Application object. 应用程序模块/组件:仅用于上下文和Application对象。
  • Net module/component: retrofit client 网络模块/组件:改造客户端
  • City module/component: a module/component to inject dependencies in a MVP screen. 城市模块/组件:在MVP屏幕中注入依赖关系的模块/组件。 I want to inject the Presenter and the Interactor in the Fragment. 我想在片段中注入Presenter和Interactor。
  • PlaceRequests: a retrofit interface PlaceRequests:改造界面

This is how the code looks: 这是代码的样子:

ApplicationModule.java ApplicationModule.java

@Module
public class ApplicationModule {
    private Application mApp;

    public ApplicationModule(Application app) {
        mApp = app;
    }

    @Provides
    @Singleton
    public Application provideApplication() {
        return mApp;
    }

    @Provides
    @Singleton
    Context provideApplicationContext() {
        return mApp.getApplicationContext();
    }
}

ApplicationComponent.java ApplicationComponent.java

@Singleton
@Component(modules = {ApplicationModule.class})
public interface ApplicationComponent {
    Application application();
    Context getContext();
}

NetModule.java NetModule.java

@Module
public class NetModule {

    String mBaseUrl;

    public NetModule(String baseUrl) {
        this.mBaseUrl = baseUrl;
    }

    @Provides
    @Singleton
    SharedPreferences providesSharedPreferences(Application application) {
        return PreferenceManager.getDefaultSharedPreferences(application);
    }

    @Provides
    @Singleton
    Cache provideOkHttpCache(Application application) {
        int cacheSize = 10 * 1024 * 1024; // 10 MiB
        return new Cache(application.getCacheDir(), cacheSize);
    }

    @Provides
    @Singleton
    OkHttpClient provideOkHttpClient(Cache cache) {
        OkHttpClient client = new OkHttpClient();
        client.setCache(cache);
        return client;
    }

    @Provides
    @Singleton
    Retrofit provideRetrofit(OkHttpClient okHttpClient) {
        return new Retrofit.Builder()
                .addConverterFactory(JacksonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .baseUrl(mBaseUrl)
                .client(okHttpClient)
                .build();
    }

    @Provides
    @Singleton
    PlaceRequests providePlaceRequests(Retrofit retrofit) {
        return retrofit.create(PlaceRequests.class);
    }
}

NetComponent.java NetComponent.java

@Singleton
@Component(
        dependencies = {ApplicationModule.class},
        modules = {NetModule.class}
)
public interface NetComponent {
    Application application();
    PlaceRequests getPlaceRequests();
}

CityModule.java CityModule.java

@Module
public class CityModule {

    private CityMvp.View view;

    public CityModule(CityMvp.View view) {
        this.view = view;
    }

    @Provides
    public CityMvp.View provideView() {
        return view;
    }

    @Provides
    public CityMvp.Interactor provideInteractor(PlaceRequests placeRequests) {
        return new CityInteractor(placeRequests);
    }

    @Provides
    public CityPresenter providePresenter(CityMvp.View cityView, CityMvp.Interactor interactor) {
        return new CityPresenter(cityView, interactor);
    }
}

CityComponent.java CityComponent.java

@PerFragment
@Component(
        dependencies = {NetModule.class},
        modules = {CityModule.class}
)

public interface CityComponent {
    void inject(CityFragment cityFragment);
}

CityInteractor.java (the one that is not able to inject cause the PlaceRequests dependency) CityInteractor.java(无法注入的那个导致PlaceRequests依赖项)

public class CityInteractor implements CityMvp.Interactor {

    private PlaceRequests placeRequests;

    public CityInteractor(PlaceRequests placeRequests) {
        this.placeRequests = placeRequests;
    }

    @Override
    public void getPlaceDetails(String placeId, String key, Subscriber<PlaceDetails> subscriber) {
        Observable<PlaceDetails> observable = placeRequests.getPlaceDetails(placeId, key);
        observable.observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(subscriber);
    }
}

Finally, the error trace: 最后,错误跟踪:

    : error: com.blabla.blabla.webapi.place.PlaceRequests cannot be provided without an @Provides-annotated method.
    void inject(CityFragment cityFragment);
         ^
      com.blabla.blabla.screens.city.CityFragment.presenter
          [injected field of type: com.blabla.blabla.screens.city.CityPresenter presenter]
      com.blabla.blabla.di.modules.CityModule.providePresenter(com.blabla.blabla.screens.city.CityMvp.View cityView, com.blabla.blabla.screens.city.CityMvp.Interactor interactor)
          [parameter: com.blabla.blabla.screens.city.CityMvp.Interactor interactor]
      com.blabla.blabla.di.modules.CityModule.provideInteractor(com.blabla.blabla.webapi.place.PlaceRequests placeRequests)
          [parameter: com.blabla.blabla.webapi.place.PlaceRequests placeRequests]
3 errors
:app:compileDebugJavaWithJavac FAILED

FAILURE: Build failed with an exception.

From what I understand, I'm exposing PlaceRequests object in NetComponent.java, and NetModule.java is a dependency of CityComponent. 根据我的理解,我在NetComponent.java中公开PlaceRequests对象,而NetModule.java是CityComponent的依赖项。 How come I'm not able to get PlaceRequests dependency from CityComponent? 为什么我无法从CityComponent获得PlaceRequests依赖? What am I missing? 我错过了什么? Thanks! 谢谢!

Component dependencies specified through @Component(dependencies={...}) shouldn't be Modules, as you have them. 通过@Component(dependencies={...})指定的组件依赖 @Component(dependencies={...})不应该是模块,因为您拥有它们。 They should be types (typically Components) that make dependencies available through zero-arg methods known as provision methods . 它们应该是类型(通常是组件),通过称为提供方法的零参数方法使依赖项可用。

Switch your dependencies to the components instead of modules. 将依赖项切换到组件而不是模块。

If it helps, you might want to change how you think about Components, Modules, and component dependencies. 如果它有帮助,您可能想要改变您对组件,模块和组件依赖关系的看法。 You can think of Dagger as creating an object graph where Modules define the inputs or bindings of the graph and Components define the outputs or consumers of the graph . 您可以将Dagger视为创建对象图,其中模块定义的输入或绑定,组件定义 的输出或使用者 This leaves component dependencies to be types or components included or imported from another external source, which might include a different Dagger-created Component, but probably wouldn't include a Module--instead of a dependency on the Module you'd depend on the Component that consumes the module. 这使组件依赖性成为包含或从另一个外部源导入的类型或组件,其可能包括不同的Dagger创建的组件,但可能不包含模块 - 而不是依赖于您依赖于模块的模块消耗模块的组件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Dagger 2-如果没有@Provides注释的方法,则无法提供 - Dagger 2 - Cannot be provided without an @Provides-annotated method 不能在没有 @Provides 注释的方法的情况下提供(Android Kotlin 中的 Dagger 2) - Cannot be provided without an @Provides-annotated method (Dagger 2 in Android Kotlin) Dagger 2:没有 @Provides 注释的方法就无法提供 - Dagger 2: Cannot be provided without an @Provides-annotated method Dagger2问题“如果没有@ Provide-annotated方法,则无法提供”。 - Dagger2 issue with “cannot be provided without an @Provides-annotated method.” Dagger Hilt Android 的编译时错误: okhttp3.Interceptor 不能在没有 @Provides-annotated 方法的情况下提供 - Compile time error with Dagger Hilt Android: okhttp3.Interceptor cannot be provided without an @Provides-annotated method 匕首/缺少绑定。 如果没有@Provides 注释的方法,则无法提供输出器 - Dagger/MissingBinding. Outputter cannot be provided without an @Provides-annotated method Dagger2-没有@Inject构造函数或@Provides注释方法无法提供 - Dagger2 - cannot be provided without an @Inject constructor or from an @Provides-annotated method 匕首-MissingBinding地图 <Class<? extends ViewModel> ,供应商 <ViewModel> &gt;如果没有@Provides注释的方法,则无法提供 - Dagger - MissingBinding Map<Class<? extends ViewModel>,Provider<ViewModel>> cannot be provided without an @Provides-annotated method 如何解决 dagger 错误 - 如果没有 @Inject 构造函数或 @Provides-annotated 方法,则无法提供字符串 - How to resolve dagger error - String cannot be provided without an @Inject constructor or an @Provides-annotated method 没有@Inject构造函数或@Provides注释方法无法提供-Dagger 2 - cannot be provided without an @Inject constructor or from an @Provides-annotated method - Dagger 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM