简体   繁体   English

SWTException:Widget已被释放

[英]SWTException: Widget is disposed

When I create open new SWT app window a second time, app crashes with SWTException: Widget is disposed error. 当我第二次创建打开的新SWT应用程序窗口时,应用程序崩溃与SWTException: Widget is disposed错误。 What's wrong? 怎么了?

Here is my code: 这是我的代码:

ABSTRACT Controller.java : 摘要Controller.java

public abstract class Controller {
    protected View view;

    public Controller(View v) {
        view = v;
    }

    protected void render() {
        data();
        view.setData(data);
        view.render();
        listeners();
        if (display)
            view.open();
    }
    protected void data() {}

    protected void listeners() {}
}

AboutController.java (represends new window): AboutController.java (代表新窗口):

public class AboutController extends Controller {
    static AboutView view = new AboutView();

    public AboutController() {
        super(view);
        super.render();
    }
}

ABSTRACT View.java : 摘要View.java

public abstract class View {
    protected Display display;
    protected Shell shell;
    protected int shellStyle = SWT.CLOSE | SWT.TITLE | SWT.MIN;

    private void init() {
        display = Display.getDefault();
        shell = new Shell(shellStyle);
    };

    protected abstract void createContents();

    public View() {
        init();
    }

    public void render() {
        createContents();
    }

    public void open() {
        shell.open();
        shell.layout();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }
}

And my view AboutView.java 我的观点是AboutView.java

public class AboutView extends View implements ApplicationConstants {

    protected void createContents() {
        shell.setSize(343, 131);
        shell.setText("About");

        Label authorImage = new Label(shell, SWT.NONE);
        authorImage.setBounds(10, 10, 84, 84);
        authorImage.setImage(SWTResourceManager.getImage(AboutView.class,
                "/resources/author.jpg"));
    }
}

When I try to create new app window, with new AboutController() then Widget is disposed error occurs. 当我尝试创建新的应用程序窗口时,使用new AboutController()然后出现Widget is disposed错误。

The problem is that you cannot access an already disposed widget. 问题是您无法访问已经放置的小部件。 In your code, AboutController.view is static, so it is created only once when the class AboutController is initialized. 在您的代码中, AboutController.view是静态的,因此在初始化类AboutController时只创建一次。 When the Shell is closed, it is automatically disposed and so all child-widgets get disposed too - including your view object. 关闭Shell ,它会自动处理,因此所有子窗口小部件也会被处理掉 - 包括您的视图对象。

When you then open the window a second time, the already disposed view is handed over to the super-constructor instead of a newly created view. 当您再次打开窗口时,已经处理的视图将被移交给超级构造函数而不是新创建的视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM