当前位置:  开发笔记 > 编程语言 > 正文

Dagger2错误:必须设置模块

如何解决《Dagger2错误:必须设置模块》经验,为你挑选了2个好方法。

我试图在Dagger2中做SubScoping.但是,我无法弄清楚这个编译错误: - > ...MyApplicationModule must be set发生在我的LogInFragment.如果有人试图对这个错误有所了解.我真的很高兴.

这是MyApplication类:

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;
    }

}

这是我尝试Subcope的地方

这是MyLogIn组件类

@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();
}

dgngulcan.. 26

该错误可能是由abstract class模块引起的.Dagger如果是抽象类,则不能使用模块.



1> dgngulcan..:

该错误可能是由abstract class模块引起的.Dagger如果是抽象类,则不能使用模块.


我使用Android Studio将模块代码从Java转换为Kotlin,并将_public class_转换为_object_,这引起了同样的问题。

2> Egor Neliuba..:

LogInComponent取决于MyApplicationComponent哪个包含MyApplicationModule.您也不应该同时声明这个模块LogInComponent.删除它,它将编译.


此外,请确保你揭露你在需要依赖LogInComponentMyApplicationComponent将它们添加到像这样的组件接口:

@Component (modules = {MyApplicationModule.class}) 
public interface MyApplicationComponent { 
    Context context();
    SharedPreferences sharedPreferences();
    // ...
}

另一个提示 - 如果您需要所有依赖项,MyApplicationComponent您可能需要阅读有关子组件的内容.

推荐阅读
ifx0448363
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有