简体   繁体   English

尝试使用Media Player时Application Constructor中的异常

[英]Exception in Application Constructor while trying to use Media Player

I have written a code which will play music when a link on a webpage is found. 我写了一个代码,当找到网页上的链接时,它将播放音乐。

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

The error I am getting is this: 我得到的错误是这样的:

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

A window seems to open when I run the application but is automatically closed and this error appears. 运行应用程序时,似乎打开了一个窗口,但该窗口自动关闭,并出现此错误。

I would really appreciate the help. 我非常感谢您的帮助。 Thank you. 谢谢。

You are starting an application from the WebCrawl class using Application.launch(String[]) , so launch tries to create a WebCrawl instance, which it can't, since WebCrawl is abstract . 您正在使用Application.launch(String[])WebCrawl类启动应用程序,因此launch尝试创建一个WebCrawl实例,因为WebCrawlabstract ,所以它无法创建该实例。

BTW: Placing code after the Application.launch call won't work, since after Application.launch is finished, the JavaFX platform will already have exited. 顺便说一句:在Application.launch调用之后放置代码将不起作用,因为在Application.launch完成之后,JavaFX平台将已经退出。

You can read about the application lifecycle in the Life-cycle section of the Application javadoc . 您可以Application javadocLife-cycle部分中了解有关应用程序生命周期的信息

You need to call the code form the start method or later. 您需要从start方法或更高版本中调用代码。

You can find a tutorial for a simple JavaFX application here: https://docs.oracle.com/javase/8/javafx/get-started-tutorial/hello_world.htm 您可以在这里找到一个简单的JavaFX应用程序的教程: https : //docs.oracle.com/javase/8/javafx/get-started-tutorial/hello_world.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM