2017-08-08 24 views
-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(); 
} 
+3

的可能的复制[如何解决匕首2错误“......不能提供\ [\]”? ](https://stackoverflow.com/questions/44912080/how-doi-i-fix-dagger-2-error-cannot-be-provided) –

+0

@DavidMedenjak这看起来不像重复。我认为主持人与它有关。 –

+1

请按照答案中的描述仔细验证您的代码。您没有包括您提供“EnergyProfileRepository”的位置或方式,也没有包含完整的注入跟踪(错误消息后面的行)。你将一个实现绑定到接口,但是如果没有@Inject构造函数或者@提供注释方法,就不能提供实现* –

回答

0

这需要在EnergyProfileRepository类添加:

@Inject 
public EnergyProfileRepository() { 
}