我在项目中玩Dagger 2,然后被卡在此错误编译中。
-> Error:(18, 21) error: ....MyManager cannot be provided without an @Provides-annotated method....MyManager is injected at...SignInPresenter.<init>(myManager)...SignInPresenter is provided at...SignInComponent.signInPresenter()
我尝试研究该主题,但无法确切指出代码中的错误。我认为我在某个地方犯了一个小错误,或者我了解了Dagger2中的某些错误。如果有人可以指出错误。我会很感激的。
我的经理
public interface MyManager {
Observable<User> getAllUsers();
}
登录演示者
@Inject
public SignInPresenter(MyManager myManager) {
this.myManager= myManager;
}
我在MySignInFragment中做类似的事情
@Override protected void injectDependencies() {
signInComponent = DaggerSignInComponent.builder()
.myApplicationComponent(MyDaggerApplication.getMyComponents())
.build();
}
登录组件
@Component(modules = {MyModule.class},
dependencies = {MyApplicationComponent.class})
public interface SignInComponent {
SignInPresenter signInPresenter();
}
这是我的申请
public class MyDaggerApplication extends Application {
private static MyApplicationComponent myApplicationComponent;
@Override
public void onCreate() {
super.onCreate();
myApplicationComponent = DaggerMyApplicationComponent.create();
myApplicationComponent = DaggerMyApplicationComponent.builder().myModule(new MyModule(this)).build();
myApplicationComponent.inject(this);
}
public MyApplicationComponent getMyAppComponents(){
return myApplicationComponent;
}
public static MyApplicationComponent getMyComponents(){
return myApplicationComponent;
}
}
我的模块和组件类
@Component(modules = {MyModule.class})
public interface MyApplicationComponent {
void inject(MyDaggerApplication myDaggerApplication);
}
@Module
public class MyModule {
private final MyDaggerApplication myDaggerApplication;
public MyModule(MyDaggerApplication myDaggerApplication){
this.myDaggerApplication = myDaggerApplication;
}
@Provides
@Singleton
Context providesApplicationContext() {
return this.myDaggerApplication;
}
@Provides
@Singleton
SharedPreferences providesSharedPreferences(Context context) {
return context.getSharedPreferences("My_Pref", Context.MODE_PRIVATE);
}
@Provides
@Singleton
public MyDefaultManager providesMyDefaultManager(MyDefaultManager myDefaultManager,Context context){
return myDefaultManager.getInstance(context);
}
}
我猜我在
DaggerApplication
中做错了什么。任何建议意见将不胜感激。 :) 最佳答案
假设MyDefaultManager
实现了MyManager
,请将MyModule
中的最终提供程序更改为:
@Provides
@Singleton
public MyManager providesMyDefaultManager(MyDefaultManager myDefaultManager,Context context){
return myDefaultManager.getInstance(context);
}
如要返回
? implements MyManager
的实例,而不是专门返回MyDefaulManager
的实例。关于android - Dagger 2问题:没有@Provides方法就无法提供,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41525968/