Dagger2 error: Cannot provide without @Inject constructor

Dagger2 error: Cannot provide without @Inject constructor

我是 Dagger 2 的新手,有一个小问题。希望你能帮我 :) 我的 android 项目

中有以下 classes
  1. 应用程序
  2. AppComponent
  3. AppModule
  4. 主要活动
  5. 主要组件
  6. 主模块
  7. IntentStarter

在 rebuilding/compiling 我得到错误

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 class 中使用构造函数进行了尝试...没有构造函数...:/ 现在一些代码...

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

}

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

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