我已经为此苦苦挣扎了几个小时。我正在尝试运行一些 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/