-1
在ProfileExtendedPresenter中,當我只將視圖(第一個參數)放入構造函數時,它就起作用。隨着第二energyProfileRepository我得到的錯誤如下:匕首2使用Presenter時不能注入構造函數的第二個參數
錯誤消息:
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();
}
的可能的複製[如何解決匕首2錯誤「......不能提供\ [\]」? ](https://stackoverflow.com/questions/44912080/how-doi-i-fix-dagger-2-error-cannot-be-provided) –
@DavidMedenjak這看起來不像重複。我認爲主持人與它有關。 –
請按照答案中的描述仔細驗證您的代碼。您沒有包括您提供「EnergyProfileRepository」的位置或方式,也沒有包含完整的注入跟蹤(錯誤消息後面的行)。你將一個實現綁定到接口,但是如果沒有@Inject構造函數或者@提供註釋方法,就不能提供實現* –