gpt4 book ai didi

java - 尝试使用媒体播放器时应用程序构造函数中出现异常

转载 作者:行者123 更新时间:2023-12-01 10:02:06 25 4
gpt4 key购买 nike

我编写了一段代码,当找到网页上的链接时,它将播放音乐。

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javafx.application.*;
// * @author Archit

public abstract class WebCrawl extends Application{

public static void main(String[] args) throws IOException {
Application.launch(args);
int a=0;
try {
Document doc = Jsoup.connect("https://in.bookmyshow.com/ranchi").get();
org.jsoup.select.Elements links = doc.select("a");
for (Element e: links) {
if ((e.attr("abs:href").equals("https://in.bookmyshow.com/ranchi/movies/fan/ET00025074"))) {
try {
File f = new File("/Users/Archit/Documents/Music/campbell.wav");
Media hit = new Media(f.toURI().toString());
MediaPlayer mediaPlayer = new MediaPlayer(hit);
mediaPlayer.play();
} catch(Exception ex) {
System.out.println("Exception");
}
}
}
}

我收到的错误是这样的:

Exception in Application constructor java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class webcrawl.WebCrawl
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:907)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.InstantiationException
at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:48)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:819)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Exception running application webcrawl.WebCrawl

当我运行应用程序时,似乎打开了一个窗口,但它自动关闭并出现此错误。

我非常感谢您的帮助。谢谢。

最佳答案

您正在使用 Application.launch(String[])WebCrawl 类启动应用程序,因此 launch 尝试创建一个 WebCrawl 实例,但它不能,因为 WebCrawl抽象

顺便说一句:在 Application.launch 调用之后放置代码将不起作用,因为在 Application.launch 完成后,JavaFX 平台将已经退出。

您可以在Life-cycle section of the Application javadoc中阅读有关应用程序生命周期的信息。 .

您需要从 start 方法或更高版本调用代码。

您可以在此处找到简单 JavaFX 应用程序的教程:https://docs.oracle.com/javase/8/javafx/get-started-tutorial/hello_world.htm

关于java - 尝试使用媒体播放器时应用程序构造函数中出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36725467/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com