gpt4 book ai didi

android - Dagger 2 问题 : cannot be provided without a @Provides method

转载 作者:行者123 更新时间:2023-11-30 00:47:43 27 4
gpt4 key购买 nike

我在我的项目中玩 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);
}

因为你想返回一个的实例?具体实现 MyManager 而不是 MyDefaulManager

关于android - Dagger 2 问题 : cannot be provided without a @Provides method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41525968/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com