Dagger2错误:没有@Inject构造函数就无法提供

时间:2015-08-26 17:09:46

标签: android dependency-injection dagger-2

我对Dagger 2全新,并且有一个小问题。希望你能帮我 :) 我的android项目中有以下类

  1. 应用
  2. AppComponent
  3. 的AppModule
  4. MainActivity
  5. MainComponent
  6. MainModule
  7. IntentStarter
  8. 在重建/编译时,我收到错误

    Error:(15, 10) error: xyz.IntentStarter cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method.
    xyz..MainActivity.intentStarter
    [injected field of type: xyz..IntentStarter intentStarter]
    

    我尝试了很多变种但没有成功......我在IntentStarter类中使用构造函数尝试了它..没有构造函数......:/ 现在有些代码...

    // AppComponent.class
    @Singleton
    @Component(modules = {AppModule.class})
    public interface AppComponent {
    // Empty...
    }
    

    ...

    // AppModule.class
    @Singleton
    @Module
    public class AppModule {
    
        Application application;
        Context context;
    
    
        public AppModule(Application app) {
            this.application = app;
            this.context = app.getApplicationContext();
        }
    
    
        @Provides
        public Application provideApplication() {
            return application;
        }
    
        @Provides
        public Context provideContext() {
            return context;
        }
    
        @Provides
        public EventBus provideEventBus() {
            return EventBus.getDefault();
        }
    
        @Provides
        public IntentStarter provideIntentStarter() {
            return new IntentStarter();
        }
    }
    

    ...

    // App.class
    public class App extends Application {
    
        public AppComponent appComponent;
    
        public AppComponent getAppComponent() {
            return appComponent;
        }
    
        @Override
        public void onCreate() {
            super.onCreate();
            appComponent = DaggerAppComponent.builder()
                .appModule(new AppModule(this))
                .build();
        }
    }
    

    ...

    //MainAcitivty.class
    public class MainActivity extends MosbyActivity {
    
        @Inject
        IntentStarter intentStarter;
    
        MainActivityComponent component;
    
        @Override
        protected void injectDependencies() {
            component = DaggerMainActivityComponent.builder()
                    .appComponent(((App) getApplication()).getAppComponent())
                    .build();
            component.inject(this);
        }
    }
    

    ...

    //MainActivityComponent.class
    @ActivityScope
    @Component(dependencies = {AppComponent.class})
    public interface MainActivityComponent {
        void inject(MainActivity mainActivity);
    }
    

    ...

    // MainActivityModule
    @Module
    public class MainActivityModule {
    
    }
    

    ...

    //IntentStarter
    public class IntentStarter {
    
        @Inject
        Context context;
    
    }
    

1 个答案:

答案 0 :(得分:4)

首先,正如我所说,您的组件中缺少您的提供方法。例如,

 @Component(modules={AppModule.class})
 @Singleton
 public interface AppComponent {
        Context context();
        IntentStarter intentStarter();
 }

 @Component(dependencies={AppComponent.class}), modules={MainActivityModule.class})
 @ActivityScope
 public interface MainActivityComponent extends AppComponent {
        //other provision methods
 }

你在使用字段注入时出错了,你的IntentStarter需要调用appComponent.inject(this)或者需要在构造函数参数中获取你的上下文。

另外,我认为@Provides带注释的方法需要@Singleton范围才能获得范围内的提供者,并且标记模块本身并没有真正做任何事情。

所以,具体来说,

@Singleton
@Component(modules = {AppModule.class})
    public interface AppComponent {
    Application application();
    Context provideContext();
    EventBus provideEventBus();
    IntentStarter provideIntentStarter();
}

@Module
public class AppModule {
    Application application;
    Context context;

    public AppModule(Application app) {
        this.application = app;
        this.context = app.getApplicationContext();
    }


    @Provides
    public Application provideApplication() {
        return application;
    }

    @Provides
    public Context provideContext() {
        return context;
    }

    @Provides
    @Singleton
    public EventBus provideEventBus() {
        return EventBus.getDefault();
    }

    @Provides
    @Singleton
    public IntentStarter provideIntentStarter(Context context) {
        return new IntentStarter(context);
    }
}

@ActivityScope
@Component(dependencies = {AppComponent.class}, modules={MainActivityModule.class})
public interface MainActivityComponent extends AppComponent {
    void inject(MainActivity mainActivity);
}

public class IntentStarter {    
    private Context context;

    public IntentStarter(Context context) {
        this.context = context;
    }   
}