GIN注入失败



我在应用程序中使用谷歌杜松子酒,除了一个案例外,它运行良好,我不明白为什么。基本上,我将创建的工具栏小部件注入到我的视图中,它运行良好。我看到我的工具栏上有所有不同的按钮,但当我单击主页按钮时,我有一个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);
    }
}

事件总线注入不起作用的按钮:

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);
    }
}

有人知道我为什么不能让它发挥作用吗?

您正在实例化HomeButton小部件,这样您就不使用GIN,并且将无法注入任何内容。

您还必须在EditorToolbar上注入HomeButton实例。由于HomeButton是必需的(不是可选的注入),请添加到构造函数中。

@Inject
public EditorToolbar(HomeButton homeButton) {
    homeBtn = homeButton;
    //... rest of the code as usual.
}

相关内容

  • 没有找到相关文章

最新更新