我在我的应用程序中使用google-gin并且它工作正常,除了一个案例,我不明白为什么。基本上,我将我创建的工具栏小部件注入到我的视图中,它工作正常。我看到我的工具栏上有所有不同的按钮但是当我点击Home按钮时,我有一个NullPointerException,这意味着eventBus为null,因此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);
}
}
有人知道为什么我不能让这个工作吗?
答案 0 :(得分:0)
您正在实例化HomeButton小部件,这样您就不会使用GIN并且无法注入任何内容。
您还必须在EditorToolbar上注入HomeButton实例。由于HomeButton是必需的(不是可选的注入),所以添加到构造函数。
@Inject
public EditorToolbar(HomeButton homeButton) {
homeBtn = homeButton;
//... rest of the code as usual.
}