Android Dagger2错误:没有@Inject构造函数无法提供
我对匕首2完全陌生,有个小问题。希望你能帮助我:) 在我的android项目中,我有以下几个类Android Dagger2错误:没有@Inject构造函数无法提供,android,dependency-injection,dagger-2,Android,Dependency Injection,Dagger 2,我对匕首2完全陌生,有个小问题。希望你能帮助我:) 在我的android项目中,我有以下几个类 应用程序 应用组件 应用模块 主要活动 主要成分 主模块 意向发起人 在重建/编译时,我得到了错误 Error:(15, 10) error: xyz.IntentStarter cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method. xyz..Main
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...
}
首先,正如我所说的,组件中缺少供应方法。比如说,
@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)
,或者需要在构造函数参数中获取您的上下文
另外,我认为@提供了注释的方法需要@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;
}
}
AppComponent中缺少您的配置方法。您的意思是什么?在我的AppComponent中,我指向我的AppModule,其中有一个为意图提供的方法Starteri我并不是真的喜欢Dagger 2,但这可能有帮助吗?(请参阅下文以查找错误-尝试在组件中添加provideIntentStarter
-方法)希望能有所帮助。很抱歉,我有牙医预约,正忙着填牙根(因此答案很简短),但是您的子范围组件无法查看AppComponent提供的依赖项,因为您没有指定AppComponent可以从其模块公开提供的内容。当我回到家,没有打电话时,我会写一个完整的答案
//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
}
@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;
}
}