使用Presenter时,不能注入构造函数的Dagger 2秒参数

时间:2017-08-08 18:59:08

标签: android dagger-2

在ProfileExtendedPresenter中,当我只将视图(第一个参数)放在构造函数中时,它可以工作。使用第二个energyProfileRepository我得到以下错误:

错误讯息:

ProfileExtendedComponent.inject(ProfileExtendedActivity)] EnergyProfileRepository cannot be provided without an @Inject constructor or from an @Provides-annotated method.
EnergyProfileRepository is injected at
RepositoryModule.energyProfileRepository(energyProfileRepository)
Repository<EnergyProfile> is injected at
ProfileExtendedPresenter.<init>(…, energyProfileRepository)
ProfileExtendedPresenter is injected at
ProfileExtendedModule.presenter(presenter)
ProfileExtendedMvp.Presenter is injected at
ProfileExtendedActivity.presenter
.ProfileExtendedActivity is injected at
.ProfileExtendedComponent.inject(activity)

我认为这与演示者有关。但注射的范围是活动。我认为这是正确的吗?我已经添加了所有文件,但不确定这是否太多,因为我不知道问题究竟发生在哪里。

@Singleton
@Component(modules = {AppModule.class, RepositoryModule.class})
public interface AppComponent {
    ProfileExtendedComponent plus(ProfileExtendedModule module);
}

@Module
public class AppModule {

    @Provides
    @AppContext
    @Singleton
    public Context context() {
        return Application.getInstance();
    }

    @Provides
    @Singleton
    public Resources resources(@AppContext Context context) {
        return context.getResources();
    }
}


@Module
public class ProfileExtendedModule {

    private ProfileExtendedMvp.View view;

    public ProfileExtendedModule(ProfileExtendedMvp.View view) {
        this.view = view;
    }

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

    @Provides
    public ProfileExtendedMvp.Presenter presenter(ProfileExtendedPresenter presenter) {
        return presenter;
    }
}



@Module
public class RepositoryModule {

    @Provides
    @Singleton
    public Repository<EnergyProfile> energyProfileRepository(EnergyProfileRepository energyProfileRepository) {
        return energyProfileRepository;
    }
}



@Subcomponent(modules = {ProfileExtendedModule.class})
public interface ProfileExtendedComponent {
    void inject(ProfileExtendedActivity activity);
}




public class ProfileExtendedActivity extends BaseActivity implements ProfileExtendedMvp.View {

    @Inject
    ProfileExtendedMvp.Presenter presenter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        injectDependencies(Application.getInstance().getAppComponent());
        presenter.init();
    }
}


public class ProfileExtendedPresenter implements ProfileExtendedMvp.Presenter {

    ProfileExtendedMvp.View view;

Repository<EnergyProfile> energyProfileRepository;

@Inject
public ProfileExtendedPresenter(ProfileExtendedMvp.View view, Repository<EnergyProfile> energyProfileRepository) {
    this.view = view;
    this.energyProfileRepository = energyProfileRepository;
}

}

public interface ProfileExtendedMvp {

    interface View extends Mvp.View {
    }

    interface Presenter extends Mvp.Presenter {

        void init();
    }
}

public class EnergyProfileRepository implements Repository<EnergyProfile> {

    @Override
    public Observable<EnergyProfile> fetch() {
        return null;
    }
}

public interface Repository<T> {
    Observable<T> fetch();
}


public class EnergyProfile implements Cacheable {
    @Json(name = "availableType")
    private Map<String, String> houseTypesContainer;
}

public interface Cacheable {
    void setCached();
    boolean isCached();
}

1 个答案:

答案 0 :(得分:0)

需要在EnergyProfileRepository类中添加:

@Inject
public EnergyProfileRepository() {
}