Android Dagger2错误:必须设置模块

Android Dagger2错误:必须设置模块,android,dagger-2,dagger,Android,Dagger 2,Dagger,我试着在《匕首2》中做替身。但是,我无法找出这个编译错误:->…必须设置MyApplicationModule,这发生在我的LogInFragment中。如果有人试图解释这个错误。我真的很高兴 这是我的应用程序类: public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); MyInjector.init

我试着在《匕首2》中做替身。但是,我无法找出这个编译错误:->
…必须设置MyApplicationModule
,这发生在我的
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;
    }

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

}
这是MyApplicationComponent类:

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

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

}
这就是我尝试子范围的地方

@Singleton
@Component(modules = {MyApplicationModule.class}, dependencies = {MyApplicationComponent.class})
public interface LogInComponent {
    LogInPresenter signInPresenter();
}
这是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();
}

您的
LogInComponent
依赖于包含
MyApplicationModule
MyApplicationComponent
。您不应该在
LogInComponent
中也声明相同的模块。删除它,它将编译


另外,通过将依赖项添加到组件接口,确保在
MyApplicationComponent
LogInComponent
中公开所需的依赖项,如下所示:

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


另一个提示–如果您需要MyApplicationComponent中的所有依赖项,您可能需要阅读。

此错误可能是由
抽象类
模块引起的。如果模块是抽象类,Dagger就不能使用它们。

我使用Android Studio将我的模块代码从Java转换为Kotlin,它将我的公共类变成了一个对象,从而导致了同样的问题。