我正在尝试新的匕首 2,这是我第一次实现它,但我无法让它工作。我想我明白了这个概念,我理解了这里的例子
我尝试复制相同的结构,只是对我的示例进行了一些修改。
这是扩展 Graph 的 AppComponent,我在其中定义了我想要的类。
@ApplicationScope
@Component(modules = {AppModule.class, DataModule.class})
public interface EFAppComponent extends EFGraph {
/**
* An initializer that creates the graph from an application.
*/
public final static class Initializer {
private Initializer() {
} // No instances.
public static EFAppComponent init(EFApp app) {
return Dagger_EFAppComponent.builder()
.appModule(new AppModule(app))
.build();
}
}
}
public interface EFGraph {
public void inject(EFApp app);
public ImageLoader imageLoader();
public EventBus eventBus();
}
然后在每个模块中,我提供相应的类。从这里一切正常,Dagger 接缝正确构建 Dagger_EFAppComponent。
然后在我使用构造函数初始化的应用程序类中
component = EFAppComponent.Initializer.init(this);
component.inject(this);
然后我的目标是在我的活动中注入 ImageLoader 和 EventBus。为此,我创建了一个 ActivityComponent。
@ActivityScope
@Component(
dependencies = EFAppComponent.class,
modules = ActivityModule.class
)
public interface ActivityComponent {
public void inject(BaseActivity activity);
}
然后从我的活动中我称之为注入。
activityComponent = Dagger_ActivityComponent.builder()
.eFAppComponent(component)
.activityModule(new ActivityModule(this))
.build();
activityComponent.inject(this);
所以因为我在注入方法调用之后在我的活动中声明了@Inject EventBus eventBus 应该被注入。好吧,事实并非如此。
因此,在逐步调试和跟踪我的应用程序和示例之后,我意识到 Dagger_ActivityComponent 没有正确构建。
private final ActivityModule activityModule;
private final EFAppComponent eFAppComponent;
private Dagger_ActivityComponent(Builder builder) {
assert builder != null;
this.activityModule = builder.activityModule;
this.eFAppComponent = builder.eFAppComponent;
initialize();
}
如果初始化方法为空并且没有 Provider 被声明为变量。
我错过了什么吗?我整天都在努力让它发挥作用,但我没有成功。
感谢帮助。