我的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();
}
}