Dagger2错误:必须设置模块

时间:2017-01-09 15:23:00

标签: android dagger-2 dagger

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

这是 MyApplication Class

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

}

这是我尝试 Subscope

的地方

这是 MyLogIn Component 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();
}

2 个答案:

答案 0 :(得分:25)

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

答案 1 :(得分:9)

您的LogInComponent取决于MyApplicationComponent,其中包含MyApplicationModule。您也不应该在LogInComponent中声明同一个模块。删除它,它将编译。

此外,请确保通过将LogInComponent添加到组件界面,从MyApplicationComponent公开所需的依赖项,如下所示:

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

另一个提示 - 如果您需要MyApplicationComponent中的所有依赖关系,则可能需要阅读subcomponents