从另一个进程更新eclipse视图

时间:2015-06-29 13:27:34

标签: java eclipse nullpointerexception eclipse-plugin listener

我的eclipse插件扩展ViewPart并实现IConsoleLineTracker。我在Text内创建了createPartControl(Composite parent)视图,并使用lineAppended(IRegion region)page.findView(ViewID)内获取了视图。在lineAppended()内,视图上的getter方法运行正常。但是视图上的setter方法会抛出空指针异常。

我该怎么办?

public class SampleView  extends ViewPart implements IConsoleLineTracker {
public static Text viewer=null;

public static final String ID = "views.SampleView";

IConsole console;
@Override
public void dispose() {
}

@Override
public void init(IConsole console) {
    this.console=console;
}

public void createPartControl(Composite parent) {
    viewer = new Text(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
    viewer.setText("hi");        
}

@Override
public void lineAppended(IRegion region) {
       try {
        String line = console.getDocument().get(region.getOffset(), region.getLength());

        IViewPart part=getPage().findView(".views.SampleView");

        if (part instanceof SampleView) {
          viewer = ((SampleView) part).viewer ;
          viewer.getDocument();    //works fine when I write the content to file
          viewer.setText("hi hi"); //throws NullPointerException
        }

       } catch (Exception e) {
           e.printStackTrace();
       }
}
public void setFocus() {
    viewer.setFocus();
}

}

0 个答案:

没有答案