2017-01-09 86 views
3

我試圖在Dagger2中進行SubScoping。但是,我無法弄清這個編譯錯誤: - >...MyApplicationModule must be set發生在我的LogInFragment中。如果有人會試圖對這個錯誤投下一些信息。我真的很高興。Dagger2錯誤:模塊必須設置

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

} 

這是我試圖子範圍

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

回答

4

LogInComponent取決於MyApplicationComponent包含MyApplicationModule。你也不應該在LogInComponent中聲明這個相同的模塊。刪除它,它會編譯。


此外,還要確保你揭露你在LogInComponentMyApplicationComponent需要依賴它們添加到組件接口,像這樣:

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

另一個技巧 - 如果你需要所有的依賴MyApplicationComponent您可能想要閱讀關於subcomponents

16

在我的情況下,錯誤是由abstract class模塊造成的。如果模塊是抽象的,模塊不能被Dagger使用。