本文介绍了如何注入EPartService的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发e4应用程序。我想在注入EPartService时将EPartService注入Part和处理程序
之外,然后会出现空指针错误

I am developing e4 application. I want to inject EPartService outside the Part and Handlerwhen i am injecting EPartService then i will get null pointer error

  public class DisplayRuntimePart {
          @Inject  EPartService partService;
          private void displayPart(){
          MPart part=partService.findPart("com.rcpe4.myproject.part.datapart");                   
          mpart.setVisible(true);
          partService.showPart(mpart, PartState.CREATE);   
    }
  }

我也读过这个问题,但是直到解决不了我问题

I am also read this question but till not solve my problem E4 EPartService findPart() throwing java.lang.Null Pointer Exception

编辑
我在零件类中注入了EPartService。 Application.e4xml中的类URI是此类中的bundleclass://://com.abc.test/com.abc.test.part.MyPart。

EditI am inject EPartService in Part class. Class URI in Application.e4xml is bundleclass://com.abc.test/com.abc.test.part.MyPart in this class I am write code as follows.

      Class Mypart{ 
          @Inject EPartService prtservice;                  
          @Inject
          public MyPart() {
          } 
          @PostConstruct
          public void postConstruct(Composite parent) {
             parent.setLayout(new  FillLayout(SWT.HORIZONTAL));     
             htmlBrowser = new Browser(parent, SWT.NONE);

         }  
         @PreDestroy
        public void preDestroy() {

        }   
        @Focus
        public void onFocus() {

        }   
        @Persist
        public void save() {

        } 
         public dispalyPart(){  
                MPart mpart=partService.findPart("com.abc.test.part.datapart"); **Here Getting Null Pointer Exception**
                mpart.setVisible(true);
                partService.showPart(mpart, PartState.CREATE);  
        }
   }


推荐答案

Eclipse仅对它知道的对象进行直接注入-基本上是应用程序模型(e4xmi)文件中提及的对象或使用 EPartService.showPart 之类的对象创建的对象。

Eclipse only does direct injection on objects that it 'knows' about - basically objects mentioned in the application model (e4xmi) files or created using something like EPartService.showPart.

如果要对创建的对象进行直接注入,则需要使用 ContextInjectionFactory 创建它们。例如:

If you want to do direct injection on objects that you create then you need to create them using ContextInjectionFactory. For example:

@Inject IEclipseContext context;

...

MyClass myClass = ContextInjectionFactory.make(MyClass.class, context);

您还可以使用以下常规方法对通过类创建的类进行注入:

you can also do injection on a class created in the normal way with:

ContextInjectionFactory.inject(myClass, context);

(这不会对构造函数进行注入)。

(this will not do injection on the constructor).

注意:由于此代码使用直接注入,因此必须从Eclipse应用程序模型确实了解的类(例如命令处理程序或MPart)中运行它。

Note: Since this code is using direct injection you must run it from a class that the Eclipse application model does know about such as a command handler or an MPart.

这篇关于如何注入EPartService的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 01:15