繁体   English   中英

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

[英]SWT java.lang.IllegalArgumentException: Argument cannot be null

我已经为此奋斗了几个小时。 我正在尝试运行一些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() ,消息框对话框正在关闭中,并且没有外壳处于活动状态。 您可以在打开消息框之前尝试对外壳进行缓存,并将相同的对话框用于文件对话框。

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

Display.getCurrent().getActiveShell()

我需要更多代码来解决这个问题,所以我对环境没有太多了解,但是我会尝试这样做:

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

得到一个新的壳

暂无
暂无

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

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