【发布时间】:2015-04-09 16:17:18
【问题描述】:
我在我的应用程序中使用 google-gin,它工作正常,除了一种情况,我不明白为什么。基本上,我将我创建的工具栏小部件注入到我的视图中,它工作正常。我看到带有所有不同按钮的工具栏,但是当我单击 Home 按钮时,我有一个 NullPointerException,这意味着 eventBus 为空,因此 HomeButton 中的注入不起作用。
我的看法:
public MyView extends AbstractView{
@Inject
EditorToolbar toolbar;
// ...
}
我的工具栏小部件:
public class EditorToolbar extends HorizontalLayoutContainer {
private HomeButton homeBtn;
private ToolbarIconButton saveButton;
private final static int BUTTON_RIGHT_MARGIN=5;
public EditorToolbar() {
homeBtn = new HomeButton();
saveButton = new ToolbarIconButton("Save",AppImages.INSTANCE.saveBW());
this.add(homeBtn, new HorizontalLayoutData(-1, -1, new Margins(0, BUTTON_RIGHT_MARGIN, 0, 0)));
this.add(saveButton, new HorizontalLayoutData(-1, -1, new Margins(0, BUTTON_RIGHT_MARGIN, 0, 0)));
}
public HandlerRegistration addSaveHandler(SelectEvent.SelectHandler handler){
return saveButton.addSelectHandler(handler);
}
}
eventBus的注入不起作用的按钮:
public class HomeButton extends ToolbarIconButton {
@Inject
private MetadataEditorEventBus eventBus;
public HomeButton() {
super("Back to home page", AppImages.INSTANCE.home());
setToolTip("Navigate back to the home page");
setWidth(130);
bindUI();
}
private void bindUI() {
addSelectHandler(new SelectEvent.SelectHandler() {
@Override
public void onSelect(SelectEvent selectEvent) {
eventBus.fireBackToHomePageEvent();
}
});
}
}
我的 GIN 模块:
public class MetadataEditorGinModule extends AbstractGinModule {
@Override
protected void configure() {
bind(com.google.web.bindery.event.shared.EventBus.class).to(MetadataEditorEventBus.class);
// bind(com.google.gwt.event.shared.EventBus.class).to(MetadataEditorEventBus.class);
bind(MetadataEditorEventBus.class).in(Singleton.class);
bind(com.google.web.bindery.requestfactory.shared.RequestFactory.class).to(MetadataEditorRequestFactory.class);
bind(com.google.gwt.place.shared.PlaceController.class).toProvider(PlaceControllerProvider.class).in(Singleton.class);
bind(ActivityDisplayer.class).to(MetadataEditorAppDisplayer.class).in(Singleton.class);
bind(MetadataEditorAppView.class).in(Singleton.class);
}
}
有人知道为什么我不能让它工作吗?
【问题讨论】:
-
MetadataEditorEventBus 是类还是接口?
-
这是一个类。它扩展了 SimpleEventBus。
-
你有什么 Guice 模块在玩 MetadataEditorEventBus 吗?
-
只有 gin 模块。用模块更新了我的帖子。