SWT java.lang.IllegalArgumentException:参数不能为null

时间:2012-03-14 20:04:33

标签: 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,所以我不知道这里发生了什么。你能建议吗?

很多人。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

你调试了吗?从这里看来这是空的:

Display.getCurrent().getActiveShell()

我需要更多的代码来解决这个问题,所以我没有太多关于这个问题的信息,但我会尝试这个:

Shell newShell = new Shell(Display.getCurrent() == null ? Display.getDefault() : Display.getCurrent(), SWT.NO_TRIM )

获取新shell

相关问题