Java FXexception(媒体播放器应用程序)
我刚刚开始使用Java FX而且在我的生活中无法弄清楚为什么这个exception不断发生。
我有2节课。 一个叫做Main,另一个叫做Player。 这是他们每个人的样子….
主类
package application; import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.layout.BorderPane; import javafx.scene.paint.Color; public class Main extends Application { @Override public void start(Stage primaryStage) { Player player = new Player("/Applications/Nacho.mp4"); Scene scene = new Scene (player, 720, 480, Color.BLACK); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } }
球员类
package application; import javafx.scene.layout.BorderPane; import javafx.scene.layout.Pane; import javafx.scene.media.Media; import javafx.scene.media.MediaPlayer; import javafx.scene.media.MediaView; public class Player extends BorderPane { Media media; MediaPlayer player; MediaView view; Pane nPane; public Player(String file){ media = new Media(file); player = new MediaPlayer(media); view = new MediaView(player); nPane = new Pane(); nPane.getChildren().add(view); setCenter(nPane); player.play(); } }
以下是编译器在运行程序时吐出的内容……
开始
Application start方法中的exception 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) 在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) 在java.lang.reflect.Method.invoke(Method.java:497) 在sun.launcher.LauncherHelper $ FXHelper.main(LauncherHelper.java:767) 引起:java.lang.RuntimeException:Application start方法中的exception 在com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917) at com.sun.javafx.application.LauncherImpl.lambda $ launchApplication $ 152(LauncherImpl.java:182) at com.sun.javafx.application.LauncherImpl $$ Lambda $ 51 / 1323468230.run(Unknown Source) 在java.lang.Thread.run(Thread.java:745) 引起:java.lang.IllegalArgumentException:uri.getScheme()== null! uri =='Applications / Nacho.mp4' 在com.sun.media.jfxmedia.locator.Locator。(Locator.java:211) 在javafx.scene.media.Media。(Media.java:391) 在application.Player。(Player.java:16) 在application.Main.start(Main.java:13) at com.sun.javafx.application.LauncherImpl.lambda $ launchApplication1 $ 159(LauncherImpl.java:863) at com.sun.javafx.application.LauncherImpl $$ Lambda $ 54 / 1046396900.run(Unknown Source) at com.sun.javafx.application.PlatformImpl.lambda $ runAndWait $ 172(PlatformImpl.java:326) at com.sun.javafx.application.PlatformImpl $$ Lambda $ 47 / 186276003.run(Unknown Source) at com.sun.javafx.application.PlatformImpl.lambda $ null $ 170(PlatformImpl.java:295) at com.sun.javafx.application.PlatformImpl $$ Lambda $ 49 / 287662571.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at com.sun.javafx.application.PlatformImpl.lambda $ runLater $ 171(PlatformImpl.java:294) at com.sun.javafx.application.PlatformImpl $$ Lambda $ 48 / 237061348.run(Unknown Source) at com.sun.glass.ui.InvokeLaterDispatcher $ Future.run(InvokeLaterDispatcher.java:95) 运行应用程序应用程序的exception。主要
完
我知道只要在Main中实例化Player对象就会出现问题。 一旦Player对象使用它的构造函数并创建一个Media对象,它就会传递String文件位置,就在我收到错误时。 我使用Player对象的空构造函数运行程序,它运行正常所以问题显然在我的Player类中,再次实例化Media对象时。 我传递了正确的参数吗? “/Applications/Nacho.mp4”
如果有人可以请告诉我如何解决这个问题,我将不胜感激。 否则,我无法继续推进这个项目。 谢谢!
没关系,我想通了,问题是将参数传递给Media对象。 我已经习惯了C ++语法,并没有意识到这样做的正确方法是
media = new Media("file:///Applications/Nacho.mp4");
由于错误输入文件地址,通常会发生此错误。 写入地址的正确方法是file:///C:/abc.mp4
。
更清楚:
Object b = new Object("file:///C:/abc.mp4");