Dagger2 Error: Module Must Be Set
Dagger2 Error: Module Must Be Set
我试图在 Dagger2 中做子范围。但是,我无法弄清楚这个编译错误:-> ...MyApplicationModule must be set
发生在我的 LogInFragment
中。如果有人会尝试阐明此错误。我真的很高兴。
这是 MyApplication Class:
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
MyInjector.initialize(this);
}
}
这是 MyInjector Class:
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 Class:
@Component (modules = {MyApplicationModule.class})
public interface MyApplicationComponent {
}
这是 MyApplicationModule Class
@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;
}
}
这是我尝试 Subscope
的地方
这是 MyLogIn 组件 Class
@Singleton
@Component(modules = {MyApplicationModule.class}, dependencies = {MyApplicationComponent.class})
public interface LogInComponent {
LogInPresenter signInPresenter();
}
这就是 Compilation Error
发生的地方
这是MyLogInActivityFragment
@Override protected void injectDependencies() {
logInComponent = DaggerLogInComponent.builder()
.myApplicationComponent(MyInjector.get())
.build();
}
您的 LogInComponent
取决于包含 MyApplicationModule
的 MyApplicationComponent
。您也不应该在 LogInComponent
中声明相同的模块。删除它,它将编译。
另外,通过将它们添加到组件接口,确保在 MyApplicationComponent
中公开 LogInComponent
中所需的依赖项,如下所示:
@Component (modules = {MyApplicationModule.class})
public interface MyApplicationComponent {
Context context();
SharedPreferences sharedPreferences();
// ...
}
另一个提示——如果您需要 MyApplicationComponent
中的所有依赖项,您可能需要阅读 subcomponents。
错误可能是由 abstract class
模块引起的。 Dagger 不能使用抽象的模块 类.
我试图在 Dagger2 中做子范围。但是,我无法弄清楚这个编译错误:-> ...MyApplicationModule must be set
发生在我的 LogInFragment
中。如果有人会尝试阐明此错误。我真的很高兴。
这是 MyApplication Class:
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
MyInjector.initialize(this);
}
}
这是 MyInjector Class:
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 Class:
@Component (modules = {MyApplicationModule.class})
public interface MyApplicationComponent {
}
这是 MyApplicationModule Class
@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;
}
}
这是我尝试 Subscope
的地方这是 MyLogIn 组件 Class
@Singleton
@Component(modules = {MyApplicationModule.class}, dependencies = {MyApplicationComponent.class})
public interface LogInComponent {
LogInPresenter signInPresenter();
}
这就是 Compilation Error
发生的地方
这是MyLogInActivityFragment
@Override protected void injectDependencies() {
logInComponent = DaggerLogInComponent.builder()
.myApplicationComponent(MyInjector.get())
.build();
}
您的 LogInComponent
取决于包含 MyApplicationModule
的 MyApplicationComponent
。您也不应该在 LogInComponent
中声明相同的模块。删除它,它将编译。
另外,通过将它们添加到组件接口,确保在 MyApplicationComponent
中公开 LogInComponent
中所需的依赖项,如下所示:
@Component (modules = {MyApplicationModule.class})
public interface MyApplicationComponent {
Context context();
SharedPreferences sharedPreferences();
// ...
}
另一个提示——如果您需要 MyApplicationComponent
中的所有依赖项,您可能需要阅读 subcomponents。
错误可能是由 abstract class
模块引起的。 Dagger 不能使用抽象的模块 类.