[英]MediaPlayer getting Null Pointer Exception when do tap on BACK button
[英]Getting an exception when adding audio to mediaplayer
因此,在我添加了要添加的圖像之后,我去嘗試添加這樣的音頻
<MediaPlayer fx:id="gameIntro" autoPlay="true"volume="0.1">
<media>
<Media source="@AudioFiles/GameIntroTheme.MP3" />
</media>
</MediaPlayer>
但這沒用。
與問題有關的錯誤
Caused by: javafx.fxml.LoadException:
file:/C:/Users/Owner/Documents/NetBeansProjects/MillionaireTriviaGame/dist/run30649974/MillionaireTriviaGame.jar!/millionairetriviagame/MenulayoutFXML.fxml:18
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2605)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2547)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2445)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3218)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3179)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3152)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3128)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3108)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3101)
at millionairetriviagame.MillionaireTriviaGame.start(MillionaireTriviaGame.java:17)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(LauncherImpl.java:863)
at com.sun.javafx.application.LauncherImpl$$Lambda$53/1270092040.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl$$Lambda$46/355629945.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/1753953479.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$47/1915503092.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101)
at com.sun.glass.ui.win.WinApplication$$Lambda$36/1963387170.run(Unknown Source)
... 1 more
Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[18,49]
Message: Element type "MediaPlayer" must be followed by either attribute specifications, ">" or "/>".
at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(XMLStreamReaderImpl.java:601)
at javax.xml.stream.util.StreamReaderDelegate.next(StreamReaderDelegate.java:88)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2517)
... 22 more
Exception running application millionairetriviagame.MillionaireTriviaGame
Java Result: 1
我的FXML文件
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.media.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<StackPane fx:id="MainMenu" xmlns:fx="http://javafx.com/fxml/1" fx:controller="millionairetriviagame.MenulayoutFXMLController">
<ImageView>
<image>
<Image url="@ImageFiles/BlueBackgroundColor.jpg"/>
</image>
</ImageView>
<MediaPlayer fx:id="gameIntro" autoPlay="true"volume="0.1">
<media>
<Media source="@AudioFiles/GameIntroTheme.MP3" />
</media>
</MediaPlayer>
<VBox fx:id="MainMenuLayout" spacing="20" alignment="TOP_CENTER" >
<ImageView>
<image>
<Image url="@ImageFiles/MillionaireLogo1.png"/>
</image>
</ImageView>
</VBox>
</StackPane>
我的主要班級
package millionairetriviagame;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class MillionaireTriviaGame extends Application
{
@Override
public void start(Stage menuStage) throws Exception
{
Parent object = FXMLLoader.load(getClass().getResource("MenulayoutFXML.fxml"));
Scene menuScene = new Scene(object);
menuStage.setTitle("Let's play who wants to be a millionaire");
menuStage.setScene(menuScene);
menuStage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
我的項目目錄的屏幕截圖
我嘗試使用以下代碼將媒體加載到媒體播放器中
@Override
public void initialize(URL url, ResourceBundle rb) {
Media gameIntroTheme = new Media(getClass().getResource("AudioFiles/GameIntroTheme.MP3").toExternalForm());
MediaPlayer mediaPlayer = new MediaPlayer(gameIntroTheme);
mediaPlayer.setAutoPlay(true);
mediaPlayer.setVolume(0.1);
}
它不起作用,並提供了NullpointerException
和javafx.fxml.loadException
。 但是如果我將音頻文件文件夾移到src
文件夾之外,請執行此操作
Media gameIntroTheme = new Media(new File("AudioFiles/GameIntroTheme.MP3").toURI().toString());
該程序有效。 請解釋。
所以這就是答案
Media gameIntroTheme = new Media(getClass().getResource("/millionairetriviagame/AudioFiles/GameIntroTheme.mp3").toExternalForm());
在這種情況下,請確保擴展名與正在使用的文件相同。
感謝kiheru和itachi幫助我解決了這個問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.