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

android–Dagger2错误:没有@Inject构造函数就无法提供,第1张

概述我对Dagger 2全新,并且遇到一个小问题.希望你能帮我 :)我的android项目中有以下类> App> AppComponent> AppModule> MainActivity> MainComponent> MainModule> IntentStarter在重建/编译时我得到错误Error:(15, 10

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

> App
> AppComponent
> AppModule
> MainActivity
> MainComponent
> MainModule
> 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]

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

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

// AppModule.class@Singleton@Modulepublic 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.classpublic 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.classpublic 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@Modulepublic class MainActivityModule {}

//IntentStarterpublic 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();}@Modulepublic 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;    }   }
总结

以上是内存溢出为你收集整理的android – Dagger2错误:没有@Inject构造函数就无法提供全部内容,希望文章能够帮你解决android – Dagger2错误:没有@Inject构造函数就无法提供所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1149713.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存