java - SWT java.lang.IllegalArgumentException : Argument cannot be null

标签 java swt

我已经为此苦苦挣扎了几个小时。我正在尝试运行一些 swt 代码,但是发生了这个 IllegalArgumentException,而且我不知道为什么。

private void loadLogFromFile() {
    MessageBox mbox = new MessageBox(Display.getCurrent().getActiveShell(),
            SWT.YES | SWT.NO | SWT.ICON_QUESTION);
    mbox.setText("Confirmation");
    mbox.setMessage ("This operation will clear de current selected device and " +
                  "all logs currently being displayed. Do you wish to continue?");
    final int mboxResult = mbox.open();
    if (mboxResult != SWT.YES) {
        return;
    }    

    final String fName = getLogFileImportLocation();
    if (fName == null) {
        return;
    }
}

getLogFileImportLocation 代码是:

private String getLogFileImportLocation() {
    FileDialog fd = new FileDialog(Display.getCurrent().getActiveShell(), SWT.OPEN);

    fd.setText("Load Log..");
    fd.setFileName("log.txt");

    if (mLogFileImportFolder == null) {
        mLogFileImportFolder = System.getProperty("user.home");
    }    
    fd.setFilterPath(mLogFileImportFolder);

    fd.setFilterNames(new String[] {
            "Text Files (*.txt)"
    });  
    fd.setFilterExtensions(new String[] {
            "*.txt"
    });  

    String fName = fd.open();
    if (fName != null) {
        mLogFileImportFolder = fd.getFilterPath();  /* save path to restore on future calls */
    }    

    return fName;
}

线路

FileDialog fd = new FileDialog(Display.getCurrent().getActiveShell(), SWT.OPEN);

总是给予

java.lang.IllegalArgumentException: Argument cannot be null  
at org.eclipse.swt.SWT.error(Unknown Source)  
at org.eclipse.swt.SWT.error(Unknown Source)  
at org.eclipse.swt.SWT.error(Unknown Source)  
at org.eclipse.swt.widgets.Dialog.error(Unknown Source)  
at org.eclipse.swt.widgets.Dialog.checkParent(Unknown Source)  
at org.eclipse.swt.widgets.Dialog.<init>(Unknown Source)  
at org.eclipse.swt.widgets.FileDialog.<init>(Unknown Source)  

如果我更改先调用 getLogFileImportLocation 并随后显示 MessageBox 的顺序,则问题会发生在 MessageBox 初始化中。

我对 swt 很菜鸟,所以我不知道这里发生了什么。你能给建议吗?

非常感谢。

最佳答案

这似乎是一个计时问题(可能在 Linux 上)。我猜当你调用 getActiveShell() 的那一刻对于文件对话框,消息框对话框正在关闭,并且没有任何 shell 处于 Activity 状态。您可以尝试在打开消息框之前缓存 shell,并在文件对话框中使用相同的 shell。

关于java - SWT java.lang.IllegalArgumentException : Argument cannot be null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9709426/

相关文章:

java - ArrayList、for 循环和 if 语句的问题 - 打印列表中每个项目的行

java - log4j 打印所有级别

java - 在 jSTL 中迭代映射时出错

swt - 以编程方式滚动 SWT 表

java - 在 JFace 对话框的中心添加确定按钮

java - SWT : how to make rounded border label

java - 我们什么时候真正需要为我们的 Java 代码使用 hibernate?

java - 使用 Cloud Foundry 的弹性运行时容器进行 Jenkins 构建

java - 使用 SWT 进行快速像素绘图?

java - 图标外壳上的 SWT 气球工具提示