我在MainViewController中有以下代码
public void initialize() {
// Init
Config.init();
lvOnlineDevices.setItems(Config.OnlineDevicesList);
}
在Config中:
public static ObservableList<Device> OnlineDevicesList = FXCollections.observableArrayList();
public static DeviceSearcher deviceSearcher = new DeviceSearcher("192.168.180");
public static void init() {
deviceSearcher.start();
}
public void run() {
for (int i=1;i<10;i++){
String tmpIp = _host+i;
Config.OnlineDevicesList.add(new Device(tmpIp));
}
}
我的问题是,我得到以下错误:
java.lang.IllegalStateException:不在FX应用程序线程上; currentThread = Thread-3
//更新1解决方案:
在DeviceSearcher中而不是
Config.OnlineDevicesList.add(new Device(tmpIp));
使用
Platform.runLater(() -> Config.OnlineDevicesList.add(new Device(host)));