尝试在片段中使用 Toothpick DI 时出现奇怪的错误:
othpick.registries.NoFactoryFoundException:找不到类 android.app.Application 的工厂。检查该类是否具有 @Inject 带注释的构造函数或包含 @Inject 带注释的成员。如果使用注册表,请检查它们是否使用注释处理器参数正确设置。
我的片段:
public class ApplicationMenu extends SidebarFragment {
@Inject ApplicationsService applicationsService;
@Inject SectionsService sectionsService;
private EventBus bus = EventBus.getDefault();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toothpick.inject(this, Toothpick.openScope(LauncherActivity.class)); // <- Erroring here
}
...
}
活动:
public class LauncherActivity extends SidebarActivity {
...
@Override
protected void onCreate(Bundle savedInstanceState) {
Scope scope = Toothpick.openScopes(LauncherApplication.class, LauncherActivity.class);
scope.bindScopeAnnotation(LauncherActivitySingleton.class);
scope.installModules(new LauncherActivityModule(this));
super.onCreate(savedInstanceState);
Toothpick.inject(this, scope);
setContentView(R.layout.activity_launcher);
ButterKnife.bind(this);
...
}
...
}
奇怪的是我只在片段中得到错误,其他地方的所有注入(ViewRenderers,适配器,服务等)都可以正常工作,没有问题