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

Tob*_*ias 5 android dependency-injection dagger-2

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

  1. 应用
  2. AppComponent
  3. 的AppModule
  4. 主要活动
  5. MainComponent
  6. MainModule
  7. IntentStarter

在重建/编译时我得到错误

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]
Run Code Online (Sandbox Code Playgroud)

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

// AppComponent.class
@Singleton
@Component(modules = {AppModule.class})
public interface AppComponent {
// Empty...
}
Run Code Online (Sandbox Code Playgroud)

...

// 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

...

// 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

...

//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);
    }
}
Run Code Online (Sandbox Code Playgroud)

...

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

...

// MainActivityModule
@Module
public class MainActivityModule {

}
Run Code Online (Sandbox Code Playgroud)

...

//IntentStarter
public class IntentStarter {

    @Inject
    Context context;

}
Run Code Online (Sandbox Code Playgroud)

Epi*_*rce 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
 }
Run Code Online (Sandbox Code Playgroud)

而且您在字段注入方面犯了一个错误,您的 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;
    }   
}
Run Code Online (Sandbox Code Playgroud)