JAVA 不兼容的类型:对象无法转换为我的类型
JAVA incompatible types: Object cannot be converted to my type
我试图通过在单独的线程上完成工作并返回所需的对象来更改 JavaFX 中的 GUI。但是,在完成工作并触发 task.setOnSucceeded() 之后,我尝试检索创建的对象并得到错误 "incompatible types: Object cannot be converted to type VideoScrollPane".
我认为这与原始类型有关,因为它发生在监听器中,但环顾四周后我找不到我正在寻找的建议。
如能提供任何信息,我们将不胜感激。
Task task = new Task<VideoScrollPane>() {
VideoScrollPane vsp;
@Override protected VideoScrollPane call() {
try {
System.out.print("thread...");
ExecutorService executor = Executors.newCachedThreadPool();
Future<VideoScrollPane> future = executor.submit(new Callable<VideoScrollPane>() {
@Override public VideoScrollPane call() {
return new VideoScrollPane(mediaview, vboxCentre, username, project);
}
});
vsp = future.get();
} catch(Exception exception) { System.out.println(exception.getMessage()); }
return vsp;
}
};
new Thread(task).start();
task.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
@Override public void handle(WorkerStateEvent t) {
System.out.println("complete");
try {
//where the problem occurs
VideoScrollPane v = task.get();
} catch(Exception exception) { System.out.println(exception.getMessage()); }
}
});
这是因为 task.get()
正在 returning 类型 Object
的值,但您试图将其分配给 v,它是一个 VideoScrollPane
.您可以通过强制转换来防止错误,就像这样
VideoScrollPane v = (VideoScrollPane)task.get();
请注意,如果 task.get()
return 不是 VideoScrollPane
,您将得到 ClassCastException
。
但是,如果您想完全避免此问题,请考虑通过包含泛型参数的类型来修复 task
的声明。您可以将其更改为,
Task<VideoScrollPane> task = new Task<VideoScrollPane>() {
这样,task.get()
现在将 return 变成 VideoScollPane
,您将不需要强制转换。
您的 task.get();
的 return 类型是 Object
而不是 VideoScrollPane
,将其更改为:
VideoScrollPane v = (VideoScrollPane) task.get();
您 Task
声明错误。你需要
Task<VideoScrollPane> task = new Task<VideoScrollPane>() { ... }
我试图通过在单独的线程上完成工作并返回所需的对象来更改 JavaFX 中的 GUI。但是,在完成工作并触发 task.setOnSucceeded() 之后,我尝试检索创建的对象并得到错误 "incompatible types: Object cannot be converted to type VideoScrollPane".
我认为这与原始类型有关,因为它发生在监听器中,但环顾四周后我找不到我正在寻找的建议。
如能提供任何信息,我们将不胜感激。
Task task = new Task<VideoScrollPane>() {
VideoScrollPane vsp;
@Override protected VideoScrollPane call() {
try {
System.out.print("thread...");
ExecutorService executor = Executors.newCachedThreadPool();
Future<VideoScrollPane> future = executor.submit(new Callable<VideoScrollPane>() {
@Override public VideoScrollPane call() {
return new VideoScrollPane(mediaview, vboxCentre, username, project);
}
});
vsp = future.get();
} catch(Exception exception) { System.out.println(exception.getMessage()); }
return vsp;
}
};
new Thread(task).start();
task.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
@Override public void handle(WorkerStateEvent t) {
System.out.println("complete");
try {
//where the problem occurs
VideoScrollPane v = task.get();
} catch(Exception exception) { System.out.println(exception.getMessage()); }
}
});
这是因为 task.get()
正在 returning 类型 Object
的值,但您试图将其分配给 v,它是一个 VideoScrollPane
.您可以通过强制转换来防止错误,就像这样
VideoScrollPane v = (VideoScrollPane)task.get();
请注意,如果 task.get()
return 不是 VideoScrollPane
,您将得到 ClassCastException
。
但是,如果您想完全避免此问题,请考虑通过包含泛型参数的类型来修复 task
的声明。您可以将其更改为,
Task<VideoScrollPane> task = new Task<VideoScrollPane>() {
这样,task.get()
现在将 return 变成 VideoScollPane
,您将不需要强制转换。
您的 task.get();
的 return 类型是 Object
而不是 VideoScrollPane
,将其更改为:
VideoScrollPane v = (VideoScrollPane) task.get();
您 Task
声明错误。你需要
Task<VideoScrollPane> task = new Task<VideoScrollPane>() { ... }