【问题标题】:GIN injection failureGIN注入失败
【发布时间】: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 模块。用模块更新了我的帖子。

标签: java gwt guice gwt-gin


【解决方案1】:

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

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

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

【讨论】:

  • 谢谢。但我不明白为什么我必须在构造函数中注入 homeButton 而不是作为字段......我试过了,但它没有用。
  • 您可以注入,但您缺少@Inject 注释:-)
  • 不,我试过了,但没有用。引发了异常。我不记得确切的例外,但它告诉“推迟用餐......”。无论如何,谢谢
猜你喜欢
  • 2012-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-29
  • 2017-09-16
  • 2012-03-01
  • 1970-01-01
  • 2011-10-28
相关资源
最近更新 更多