-1

In ProfileExtendedPresenter when I only put the view (first parameter) in the Constructor, it works. With the second energyProfileRepository I get the error below:

Error message:

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)

I think it has something to do with the presenter. But the scope of the injection is Activity. This is correct I think? I've added all files but not sure if this is too much, because I don't know where the problem exactly occurs.

@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();
}
Jim Clermonts
  • 1,694
  • 8
  • 39
  • 94
  • 3
    Possible duplicate of [How do I fix Dagger 2 error '... cannot be provided \[...\]'?](https://stackoverflow.com/questions/44912080/how-do-i-fix-dagger-2-error-cannot-be-provided) – David Medenjak Aug 08 '17 at 20:54
  • @DavidMedenjak this doesn't look like a duplicate. I think the presenter has something to do with it. – Jim Clermonts Aug 09 '17 at 05:18
  • 1
    Please verify your code carefully as described in the answer. You did not include where or how you provide `EnergyProfileRepository`, nor the full injection trace (the lines after the error message). You bind an implementation to the interface, but the implementation can't be provided *without an @Inject constructor or from an @Provides-annotated method* – David Medenjak Aug 09 '17 at 06:57
  • The previous answer I wrote was wrong - but we would need the full error message and the signature of `EnergyProfileRepository` to go further with this. Please provide an [MCVE] – David Rawson Aug 09 '17 at 09:05
  • @DavidRawson I don't exactly know what to include and what not, due to not knowing what the problem exactly is. Can you check if this is okay? – Jim Clermonts Aug 09 '17 at 09:12
  • Yes, as in David Medenjak's comment you need to include an explicit `@Inject` annotated constructor inside your concretion, the `EnergyProfileRepository` class. The reason for this is you are binding the interface `Repository` to the concretion, but Dagger 2 doesn't know how to construct the concretion unless it has an explicit constructor with an `@Inject` annotation – David Rawson Aug 09 '17 at 09:14
  • @DavidRawson this solves the problem, thanks. – Jim Clermonts Aug 09 '17 at 09:21
  • 1
    @JimClermonts that's great - please consider closing this question as a duplicate of Medenjak's canonical question since the answer is contained there. This makes it easier for people to find the answer to their problem in the future – David Rawson Aug 09 '17 at 09:23

1 Answers1

0

This needs to be added in the EnergyProfileRepository class:

@Inject
public EnergyProfileRepository() {
}
Jim Clermonts
  • 1,694
  • 8
  • 39
  • 94