我试图让SplashScreen像这样SplashScreen ,但我的代码不会加载mainFxml.fxml ...所有工作良好的启动画面加载好我认为但是在加载后我得到了这个错误:
`Exception in Application start method
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at istehkakat.Istehkakat$3.changed(Istehkakat.java:163)
at istehkakat.Istehkakat$3.changed(Istehkakat.java:157)
at com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:182)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
at javafx.beans.property.ObjectPropertyBase.fireValueChangedEvent(ObjectPropertyBase.java:105)
at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:112)
at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:145)
at javafx.concurrent.Task.setState(Task.java:693)
at javafx.concurrent.Task$TaskCallable.lambda$call$443(Task.java:1420)
at javafx.concurrent.Task$TaskCallable$$Lambda$177/1990300023.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
at com.sun.javafx.application.PlatformImpl$$Lambda$52/1527683020.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291)
at com.sun.javafx.application.PlatformImpl$$Lambda$51/1174361318.run(Unknown Source)
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$45(GtkApplication.java:126)
at com.sun.glass.ui.gtk.GtkApplication$$Lambda$42/1768305536.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
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:483)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:363)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:303)
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:483)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:875)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(LauncherImpl.java:157)
at com.sun.javafx.application.LauncherImpl$$Lambda$54/1972439101.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
at istehkakat.Istehkakat.showMainStage(Istehkakat.java:130)
at istehkakat.Istehkakat.start(Istehkakat.java:103)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821)
at com.sun.javafx.application.LauncherImpl$$Lambda$124/86108265.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
at com.sun.javafx.application.PlatformImpl$$Lambda$50/1717159510.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
at com.sun.javafx.application.PlatformImpl$$Lambda$52/1527683020.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291)
at com.sun.javafx.application.PlatformImpl$$Lambda$51/1174361318.run(Unknown Source)
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$45(GtkApplication.java:126)
at com.sun.glass.ui.gtk.GtkApplication$$Lambda$42/1768305536.run(Unknown Source)
... 1 more
Exception running application istehkakat.Istehkakat
Java Result: 1`
我的主要java类是:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package istehkakat;
import controller.MainFxmlController;
import java.io.IOException;
import javafx.animation.FadeTransition;
import javafx.application.Application;
import javafx.application.Preloader;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import javafx.concurrent.Worker;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Pos;
import javafx.geometry.Rectangle2D;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.effect.DropShadow;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebView;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.WindowEvent;
import javafx.util.Duration;
/**
*
* @author combozo
*/
public class Istehkakat extends Application {
private Pane splashLayout;
private ProgressBar loadProgress;
private Label progressText;
private Stage mainStage;
private static final int SPLASH_WIDTH = 676;
private static final int SPLASH_HEIGHT = 227;
@Override
public void init() {
ImageView splash = new ImageView(new Image(getClass().getResourceAsStream("/imgs/SplashScreenIll.png")));
loadProgress = new ProgressBar();
loadProgress.setPrefWidth(SPLASH_WIDTH - 20);
progressText = new Label("تحميل بيانات البرنامج ...");
splashLayout = new VBox();
splashLayout.getChildren().addAll(splash, loadProgress, progressText);
progressText.setAlignment(Pos.CENTER);
splashLayout.setStyle("-fx-padding: 5; -fx-background-color: cornsilk; -fx-border-width:5; -fx-border-color: linear-gradient(to bottom, chocolate, derive(chocolate, 50%));");
splashLayout.setEffect(new DropShadow());
}
@Override
public void start(Stage initStage) throws Exception {
notifyPreloader(new Preloader.StateChangeNotification(Preloader.StateChangeNotification.Type.BEFORE_START));
final Task preloaderTask = new Task() {
@Override
protected Object call() throws InterruptedException {
updateMessage("Loading Chancecoin");
/* FXMLLoader fxmloader = new FXMLLoader();
Parent root = fxmloader.load(getClass().getResource("/views/mainFxml.fxml"));
MainFxmlController c = (MainFxmlController) fxmloader.getController();
Scene scene = new Scene(root);
//////////////////////
Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();
stage.getIcons().add(new Image(getClass().getResourceAsStream("/imgs/favIco.png")));
stage.setX(primaryScreenBounds.getMinX());
stage.setY(primaryScreenBounds.getMinY());
stage.setWidth(primaryScreenBounds.getWidth());
stage.setHeight(primaryScreenBounds.getHeight());
stage.setTitle("أستحــقاقــات");
/////////////////////////////////
stage.setScene(scene);
stage.show();
*/
return null;
}
};
showSplash(initStage, preloaderTask);
new Thread(preloaderTask).start();
showMainStage();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
private void showMainStage() throws IOException {
// create the scene
FXMLLoader fxmloader = new FXMLLoader();
Parent root = fxmloader.load(getClass().getResource("/views/mainFxml.fxml"));
Scene scene = new Scene(root);
//////////////////////
Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();
mainStage.getIcons().add(new Image(getClass().getResourceAsStream("/imgs/favIco.png")));
mainStage.setX(primaryScreenBounds.getMinX());
mainStage.setY(primaryScreenBounds.getMinY());
mainStage.setWidth(primaryScreenBounds.getWidth());
mainStage.setHeight(primaryScreenBounds.getHeight());
mainStage.setTitle("أستحــقاقــات");
/////////////////////////////////
mainStage.setScene(scene);
mainStage.show();
mainStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
System.exit(0);
}
});
}
private void showSplash(final Stage initStage, Task task) {
progressText.textProperty().bind(task.messageProperty());
loadProgress.progressProperty().bind(task.progressProperty());
task.stateProperty().addListener(new ChangeListener<Worker.State>() {
@Override
public void changed(ObservableValue<? extends Worker.State> observableValue, Worker.State oldState, Worker.State newState) {
if (newState == Worker.State.SUCCEEDED) {
loadProgress.progressProperty().unbind();
loadProgress.setProgress(1);
mainStage.setIconified(false);
initStage.toFront();
FadeTransition fadeSplash = new FadeTransition(Duration.seconds(1.2), splashLayout);
fadeSplash.setFromValue(1.0);
fadeSplash.setToValue(0.0);
fadeSplash.setOnFinished(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
initStage.hide();
}
});
fadeSplash.play();
}
}
});
Scene splashScene = new Scene(splashLayout);
initStage.initStyle(StageStyle.UNDECORATED);
final Rectangle2D bounds = Screen.getPrimary().getBounds();
initStage.setScene(splashScene);
initStage.setX(bounds.getMinX() + bounds.getWidth() / 2 - SPLASH_WIDTH / 2);
initStage.setY(bounds.getMinY() + bounds.getHeight() / 2 - SPLASH_HEIGHT / 2);
initStage.show();
}
}
答案 0 :(得分:1)
<强>问题强>
您已声明MainStage
,但从未对其进行初始化。因此,在以下行中使用它将在以下位置抛出NPE:
mainStage.setIconified(false);
<强>解决方案强>
您可以通过在声明时初始化mainStage
来解决此问题:
private Stage mainStage = new Stage();
或者您可以在showSplash()
内首次使用它之前对其进行初始化:
private void showSplash(final Stage initStage, Task task) {
...
mainStage = new Stage();
mainStage.setIconified(false);
...
}
答案 1 :(得分:0)
showMainStage()
方法内包含以下一行:
Scene scene = new Scene(root);
mainStage = new Stage(); // include this
这可以解决您的问题。
答案 2 :(得分:0)
我环绕闪屏,我做了一个预装载器,它对我来说非常适合我的代码:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package istehkakat.preloader;
import javafx.animation.FadeTransition;
import javafx.application.Preloader;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.ProgressBar;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.BorderPaneBuilder;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.util.Duration;
/**
* Simple Preloader Using the ProgressBar Control
*
* @author TattoApps
*/
public class Istehkakat_Preloader extends Preloader {
private ProgressBar bar;
private Stage stage;
private boolean isEmbedded = false;
public void start(Stage stage) throws Exception {
isEmbedded = (stage.getWidth() > 0);
this.stage = stage;
stage.initStyle(StageStyle.UNDECORATED);
stage.initStyle(StageStyle.TRANSPARENT);
stage.setScene(createPreloaderScene());
stage.show();
}
@Override
public void handleProgressNotification(ProgressNotification pn) {
bar.setProgress(pn.getProgress());
}
@Override
public void handleStateChangeNotification(StateChangeNotification evt) {
if (evt.getType() == StateChangeNotification.Type.BEFORE_START) {
if (isEmbedded && stage.isShowing()) {
// fade out, hide stage at the end of animation
final FadeTransition ft = new FadeTransition(Duration.seconds(5.2), stage.getScene().getRoot());
ft.setFromValue(1.0);
ft.setToValue(0.0);
final Stage s = stage;
final EventHandler<ActionEvent> eh = new EventHandler<ActionEvent>() {
public void handle(ActionEvent t) {
s.hide();
}
};
ft.setOnFinished(eh);
ft.play();
} else {
stage.hide();
}
}
}
private Scene createPreloaderScene() {
bar = new ProgressBar();
final BorderPane p = BorderPaneBuilder
.create()
.center(new ImageView(new Image(this.getClass()
.getResourceAsStream("/imgs/SplashScreenIll.png"))))
.bottom(bar).build();
return new Scene(p, 676, 207, Color.TRANSPARENT);
}
}
谢谢大家