gpt4 book ai didi

JavaFX:异常运行 application.main

转载 作者:行者123 更新时间:2023-11-29 04:21:21 25 4
gpt4 key购买 nike

我是 java 的新手,一直在使用 JavaFX 学习 GUI 的东西。我正在尝试编写一个 MediaPlayer。我的文件如下:-

Main.java:-

package application;

import javafx.application.Application;
import java.io.IOException;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.Parent;



public class Main extends Application {

@Override
public void start(Stage primaryStage) {
try {/*
UPDATED: Parent root= FXMLLoader.load(getClass().getResource("/home/User001/Templates/Java-Projects/musicPlayer/src/application/Sample.fxml"));
*/
Parent root= FXMLLoader.load(getClass().getResource("Sample.fxml"));
Scene scene= new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}catch(IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}

SampleController.java:-

package application;

import java.io.File;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.binding.Bindings;
import javafx.beans.property.DoubleProperty;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.FileChooser;

public class SampleController implements Initializable {

private String filePath;
@FXML
private MediaPlayer mp;
@FXML
private MediaView mv;
@Override
public void initialize(URL arg0, ResourceBundle arg1) {}

@FXML
public void handleButtonAction(ActionEvent event) {
FileChooser fc= new FileChooser();
FileChooser.ExtensionFilter extensionFilter= new FileChooser.ExtensionFilter("Video files with mp4 extension only.", "*.mp4");
fc.getExtensionFilters().add(extensionFilter);

File file= fc.showOpenDialog(null);
filePath= file.toURI().toString();
if(filePath!= null) {
Media media= new Media(filePath);
mp= new MediaPlayer(media);
mv.setMediaPlayer(mp);
DoubleProperty width= mv.fitWidthProperty();
DoubleProperty height= mv.fitHeightProperty();
width.bind(Bindings.selectDouble(mv.sceneProperty(), "width"));
height.bind(Bindings.selectDouble(mv.sceneProperty(), "height"));
mp.play();
}
}

@FXML public void playButton(ActionEvent event) {mp.play(); mp.setRate(1);}
@FXML public void pauseButton(ActionEvent event) {mp.pause();}
@FXML public void stopButton(ActionEvent event) {mp.stop();}
@FXML public void forwardButton(ActionEvent event) {}
@FXML public void rewindButton(ActionEvent event) {}
@FXML public void fasterButton(ActionEvent event) {mp.setRate(2);}
@FXML public void slowerButton(ActionEvent event) {mp.setRate(0.5);}
@FXML public void exitButton(ActionEvent event) {System.exit(0);}
}

示例.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.BorderPane?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.media.MediaView?>

<BorderPane maxHeight= "-Infinity" maxWidth= "-Infinity" minHeight= "-Infinity" minWidth= "-Infinity" prefHeight= "400.0" prefWidth= "600.0" xmlns= "http://javafx.com/javafx.com/javafx/8.0.141" xmlns:fx= "http://javafx.com/fxml/1" fx:controller="application.SampleController">
<bottom>
<VBox prefHeight= "29.0" prefWidth= "600.0" BorderPane.alignment="CENTER">
<children>
<HBox alignment= "TOP_CENTER" prefHeight= "100.0" prefWidth= "200.0">
<children>
<Button mnemonicParsing= "false" onAction= "#handleButtonAction" text= "Open">
<HBox.margin>
<Insets />
</HBox.margin>
</Button>
<Button mnemonicParsing= "false" onAction="#playButton" text= "Play">
<HBox.margin>
<Insets />
</HBox.margin>
</Button>
<Button mnemonicParsing= "false" onAction= "#pauseButton" text= "Pause" />
<Button mnemonicParsing= "false" onAction= "#stopButton" text= "Stop" />
<Button mnemonicParsing= "false" onAction= "#forwardButton" text= "Forward" />
<Button mnemonicParsing= "false" onAction= "#pauseButton" text= "Rewind" />
<Button mnemonicParsing= "false" onAction= "#fasterButton" text= "2X" />
<Button mnemonicParsing= "false" onAction= "#slowerButton" text= "0.5X" />
<Button contentDisplay= "CENTER" mnemonicParsing= "false" onAction= "#exitButton" text= "Exit" textAlignment= "Center" />
</children>
</HBox>
</children>
</VBox>
</bottom>
<center>
<MediaView fx:id= "mv" fitHeight= "300.0" fitWidth= "500.0" BorderPane.alignment= "CENTER" />
</center>
</BorderPane>

Now, when i try to run my Main.java it returns the following warnings and errors. EDIT[1]-UPDATED STACK

Some details i'd like to share

Prism-ES2 Error : GL_VERSION (major.minor) = 1.4
javafx.fxml.LoadException:
/home/User00/Templates/Java-Projects/musicPlayer/bin/application/Sample.fxml:32

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at application.Main.start(Main.java:17)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$null$5(GtkApplication.java:139)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.sun.javafx.fxml.BeanAdapter.coerce(BeanAdapter.java:510)
at com.sun.javafx.fxml.BeanAdapter.put(BeanAdapter.java:258)
at com.sun.javafx.fxml.BeanAdapter.put(BeanAdapter.java:54)
at javafx.fxml.FXMLLoader$Element.applyProperty(FXMLLoader.java:512)
at javafx.fxml.FXMLLoader$Element.processValue(FXMLLoader.java:363)
at javafx.fxml.FXMLLoader$Element.processPropertyAttribute(FXMLLoader.java:325)
at javafx.fxml.FXMLLoader$Element.processInstancePropertyAttributes(FXMLLoader.java:235)
at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:767)
at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2823)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532)
... 17 more
Caused by: 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:498)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at com.sun.javafx.fxml.BeanAdapter.coerce(BeanAdapter.java:506)
... 26 more
Caused by: java.lang.IllegalArgumentException: No enum constant javafx.scene.text.TextAlignment.Center
at java.lang.Enum.valueOf(Enum.java:238)
at javafx.scene.text.TextAlignment.valueOf(TextAlignment.java:32)
... 36 more
  • Openjdk version-- "1.8.0_161"
  • I'm using EclipseIDE With JavaFX extension installed.

I'd like to know what's causing these Exceptions? Specially what location has java.NullPointerException been asking for? And how can i prevent these mistakes in future?

最佳答案

JavaFX 的 StackTrace 有点大,所以您应该知道如何阅读它:

首先寻找最后一个Caused by:

Caused by: java.lang.NullPointerException: Location is required.

然后搜索您的代码及其上方的行:

at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at application.Main.start(Main.java:17)

根据此信息,您知道故障线路是:

FXMLLoader.load(getClass().getResource("/home/User001/Templates/Java-Projects/musicPlayer/src/application/Sample.fxml"));

或者您的 getClass().getResource(...) 由于路径无效而返回 null。您需要为 getResource() 提供相对于您的类文件的路径

在你的情况下 getClass().getResource("Sample.fxml") 应该足够了。


更新:

java.lang.IllegalArgumentException: No enum constant javafx.scene.text.TextAlignment.Center

在这种情况下,第一行非常有用:

javafx.fxml.LoadException: 
/home/User00/Templates/Java-Projects/musicPlayer/bin/application/Sample.fxml:32

在 Sample.fxml 中搜索 Center case-sensitive 或行号 32,您会发现:

<Button contentDisplay= "CENTER" mnemonicParsing= "false" onAction= "#exitButton" text= "Exit" textAlignment= "Center" />

Center 更改为 CENTER:

<Button contentDisplay= "CENTER" mnemonicParsing= "false" onAction= "#exitButton" text= "Exit" textAlignment= "CENTER" />

关于JavaFX:异常运行 application.main,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48960166/

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