如果没有@Inject构造函数或@ Provide-或@ Produces-annotated方法

时间:2017-10-18 03:31:35

标签: java android dependency-injection dagger-2 dagger

我是Dagger 2的新手,直到最近才取得了一些进展。我有一个片段CalculatorFragment,它将所有逻辑卸载到它的Presenter类中,该类具有接口ViewOps,因为它的构造函数已由{ {1}}。

CalculatorFragment.java

CalculatorFragment

一切正常,直到我决定将更新显示功能移到一个单独的类中并尝试将其注入public class CalculatorFragment extends Fragment implements ViewOps { @Inject PresenterOps Presenter public View onCreateView(...){ //... CalculatorApplication .getApplicationComponent(getActivity()) .getCalculatorFinancialComponent(new CalculatorModule(this)) .inject(this); //... } Presenter类与构造函数具有相同的UpdateDisplay

Presenter.java

ViewOps

我对Dagger 2的理解是我需要创建一个模块类来定义将创建要注入的依赖项的方法。下面的问题是方法public class Presenter implements PresenterOps{ @Inject TextFunctions textFunctions; @Inject ConversionFunctions conversionFunctions; //ADDING THIS INJECT ANNOTATION CAUSED THE PROBLEM!!! @Inject UpdateDisplay updateDisplay; public Presenter(ViewOps viewOps) { CalculatorApplication .getApplicationComponent(getActivity()).inject(this); } public someFunctions(){ textFunctions.doSomething(); conversionFunctions.doSomething(); updateDisplay.doSomething(); } } 。每次我构建项目时,都会调出以下错误:

  

错误:(21,10)错误:如果没有,则无法提供UpdateDisplay   @Inject构造函数或来自@ Provide-或@ Produces-annotated   方法。此类型支持成员注入,但不能隐式   提供。

我以为我已经在下面的providesUpdateDisplay()中提供了@Provides providesUpdateDisplay()带注释的方法。

CalculatorModule.java

CalculatorModule

要添加,如果我没有@Module public class CalculatorModule { private CalculatorFragment calculatorFragment; public CalculatorModule(CalculatorFragment calculatorFragment) { this.calculatorFragment = calculatorFragment; } @Provides @ActivityScope ViewOps providesViewOps() { return calculatorFragment; } @Provides @ActivityScope CalculatorFragment providesCalculatorFinancial() { return calculatorFragment; } @Provides @ActivityScope PresenterOps providesPresenterOps() { return new Presenter(calculatorFragment); } //ISN'T THIS SUPPOSE TO BE THE @Provides method //THAT PROVIDES UpdateDisplay??? @Provides @ActivityScope UpdateDisplay providesUpdateDisplay() { return new UpdateDisplay(calculatorFragment); } } ,只需在@Inject UpdateDisplay updateDisplay构造函数中创建UpdateDisplay的实例,而不是将Presenter注入UpdateDislay然后一切正常。

AppComponent

问题是我不能简单地将public Presenter(ViewOps viewOps) { new UpdateDisplay(getViewOps()); } 注入我想要的UpdateDisplay

这里是Presenter

UpdateDisplay.java

UpdateDisplay

CalculatorApplication

    public class UpdateDisplay {
        @Inject ConversionFunctions conversionFunctions;

        private final ViewOps viewOps;

        public UpdateDisplay(ViewOps viewOps) {
            CalculatorApplication
            .getApplicationComponent(viewOps.getActivity())
            .inject(this);
            this.viewOps = viewOps;
        }

        public doSomething() {
           //...
        }

这些是组件:

AppComponent.java

public class CalculatorApplication extends Application {
    private AppComponent component;

    public static AppComponent getApplicationComponent(Context context) {
        return ((CalculatorApplication) context.getApplicationContext()).component;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        component = DaggerAppComponent.builder()
                .appModule(new AppModule(this))
                .build();
    }
}

CalculatorComponent.java

@Singleton
@Component(modules = {AppModule.class})
public interface AppComponent {
    void inject(ActivityMain activityMain);
    CalculatorComponent getCalculatorFinancialComponent(CalculatorModule module);
    void inject(UpdateDisplay updateDisplay);
    void inject(Presenter presenter);
}

0 个答案:

没有答案