gpt4 book ai didi

android - Dagger2 错误 : Module Must Be Set

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:50:35 25 4
gpt4 key购买 nike

我试图在 Dagger2 中进行子范围划分。但是,我无法找出此编译错误:-> ...MyApplicationModule must be set 这发生在我的 LogInFragment 中。如果有人会尝试阐明此错误。我真的很高兴。

这是我的应用程序类:

public class MyApplication extends Application {

@Override
public void onCreate() {
super.onCreate();
MyInjector.initialize(this);
}
}

这是MyInjector 类:

public enum MyInjector {
INSTANCE;

MyApplicationComponent myApplicationComponent;


private MyInjector() {
}

public static void initialize(MyApplication myApplication) {

MyApplicationComponent myApplicationComponent = DaggerMyApplicationComponent.builder()
.myApplicationModule(new MyApplicationModule(myApplication))
.build();
INSTANCE.myApplicationComponent = myApplicationComponent;
}

public static MyApplicationComponent get() {
return INSTANCE.myApplicationComponent;
}

}

这是 MyApplicationComponent 类:

@Component (modules = {MyApplicationModule.class}) 
public interface MyApplicationComponent {

}

这是MyApplicationModule 类

@Module
public class MyApplicationModule {

private final MyApplication myApplication;

public MyApplicationModule(MyApplication myApplication) {
this.myApplication = myApplication;
}

@Singleton
@Provides
SharedPreferences providesSharedPreferences(Context context) {
return context.getSharedPreferences("My_Pref", Context.MODE_PRIVATE);
}

@Singleton
@Provides
public Context providesMyApplicationContext() {
return this.myApplication.getApplicationContext();
}

@Singleton
@Provides
public LocationManager providesLocationService(Context context) {
return (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
}

@Singleton
@Provides
public MyDatabaseManager providesMyDatabaseManager(Context context) {
return MyDatabaseManager.getInstance(context);
}

@Singleton
@Provides
public AccountSystemModel providesAccountSystemModel(Context context) {
return MyDatabaseManager.getInstance(context);
}

@Singleton
@Provides
public MyApplication providesMyApplication(){
return this.myApplication;
}

}

这是我尝试子范围

的地方

这是MyLogIn组件类

@Singleton
@Component(modules = {MyApplicationModule.class}, dependencies = {MyApplicationComponent.class})
public interface LogInComponent {
LogInPresenter signInPresenter();
}

这是编译错误发生的地方

这是MyLogInActivityFragment

@Override protected void injectDependencies() {
logInComponent = DaggerLogInComponent.builder()
.myApplicationComponent(MyInjector.get())
.build();
}

最佳答案

错误可能是由抽象类 模块引起的。如果模块是抽象类,则 Dagger 无法使用它们。

关于android - Dagger2 错误 : Module Must Be Set,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41551237/

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