我在我的项目中玩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
。