JAVA 不兼容类型:对象无法转换为我的类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29017233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 07:12:04  来源:igfitidea点击:

JAVA incompatible types: Object cannot be converted to my type

javajavafx

提问by macourtney7

I'm trying to make changes to my GUI in JavaFX by doing the work on a separate thread and returning the object required. However, after doing the work and task.setOnSucceeded() is triggered I attempt to retrieve the created object and get the error "incompatible types: Object cannot be converted to type VideoScrollPane".

我正在尝试通过在单独的线程上进行工作并返回所需的对象来更改 JavaFX 中的 GUI。但是,在完成工作并触发 task.setOnSucceeded() 后,我尝试检索创建的对象并收到错误“类型不兼容:对象无法转换为 VideoScrollPane 类型”。

I think this has something to do with raw types as it's within the listener this is happening but after looking around I couldn't find the suggestions I was looking for.

我认为这与原始类型有关,因为它在侦听器中正在发生,但环顾四周后,我找不到我正在寻找的建议。

Any light that can be shed would be much appreciated.

任何可以发出的光都将不胜感激。

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()); }
    }
});

采纳答案by Minty Fresh

This is because the task.get()is returning a value of type Object, but you're trying to assign it to v, which is a VideoScrollPane. You can prevent the error by doing a cast, like so

这是因为task.get()正在返回一个类型的值Object,但您试图将它分配给 v,这是一个VideoScrollPane。您可以通过执行强制转换来防止错误,就像这样

VideoScrollPane v = (VideoScrollPane)task.get();

Be warned, if task.get()returns something that isn't a VideoScrollPane, you'll get a ClassCastException.

请注意,如果task.get()返回不是 a 的内容VideoScrollPane,您将得到一个ClassCastException.

If you want to prevent the problem entirely however, consider fixing the declaration of task, by including a type for the generic parameter. You could change it to,

但是,如果您想完全避免该问题,请考虑task通过包含泛型参数的类型来修复 , 的声明。你可以把它改成,

Task<VideoScrollPane> task = new Task<VideoScrollPane>() {

This way, task.get()will now return a VideoScollPane, and you won't need a cast.

这样,task.get()现在将返回 a VideoScollPane,您将不需要演员表。

回答by Crazyjavahacking

The return type of your task.get();is Objectand not VideoScrollPane, change it to:

您的返回类型task.get();Object和不是VideoScrollPane,将其更改为:

VideoScrollPane v = (VideoScrollPane) task.get();

回答by James_D

You have declared your Taskincorrectly. You need

你的声明是Task错误的。你需要

Task<VideoScrollPane> task = new Task<VideoScrollPane>() { ... }