JavaFXで画面が切り替わる機能を作ろうとしています.調べて見ると,カスタムコントローラと呼ばれるコントローラとルート(シーン)を一体にしたような(?)方式で実現できるとのことでした.手始めに,最初の画面表示,つまり1ページ目をこの方式で作成しようとしたのですが,ページを表示させようとするとコントローラのインスタンスが無限に作られるようになって(ループ?)しまいました(コンストラクタの中ににSystem.out.println("AAAAA")を置いてみるとコンソールにずらずらと...).原因と解決策を教えて欲しいです.
よろしくお願いします.
AAAAA AAAAA ・ ・ ・ AAAAA AAAAA Exception in Application start method 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 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:498) 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:917) at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182) at java.lang.Thread.run(Thread.java:748) Caused by: java.lang.StackOverflowError at sun.misc.URLClassPath.access$100(URLClassPath.java:65) at sun.misc.URLClassPath$1.next(URLClassPath.java:266) at sun.misc.URLClassPath$1.hasMoreElements(URLClassPath.java:277) at java.net.URLClassLoader$3$1.run(URLClassLoader.java:601) at java.net.URLClassLoader$3$1.run(URLClassLoader.java:599) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader$3.next(URLClassLoader.java:598) at java.net.URLClassLoader$3.hasMoreElements(URLClassLoader.java:623) at sun.misc.CompoundEnumeration.next(CompoundEnumeration.java:45) at sun.misc.CompoundEnumeration.hasMoreElements(CompoundEnumeration.java:54) at sun.misc.CompoundEnumeration.next(CompoundEnumeration.java:45) at sun.misc.CompoundEnumeration.hasMoreElements(CompoundEnumeration.java:54) at java.util.ServiceLoader$LazyIterator.hasNextService(ServiceLoader.java:354) at java.util.ServiceLoader$LazyIterator.hasNext(ServiceLoader.java:393) at java.util.ServiceLoader$1.hasNext(ServiceLoader.java:474) at javax.xml.stream.FactoryFinder$1.run(FactoryFinder.java:352) at java.security.AccessController.doPrivileged(Native Method) at javax.xml.stream.FactoryFinder.findServiceProvider(FactoryFinder.java:341) at javax.xml.stream.FactoryFinder.find(FactoryFinder.java:313) at javax.xml.stream.FactoryFinder.find(FactoryFinder.java:227) at javax.xml.stream.XMLInputFactory.newInstance(XMLInputFactory.java:154) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2472) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441) at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409) at application.TestController.loadFXML(TestController.java:21) at application.TestController.<init>(TestController.java:15) at sun.reflect.GeneratedConstructorAccessor2.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at java.lang.Class.newInstance(Class.java:442) at sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:51) at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:927) at javafx.fxml.FXMLLoader$RootElement.processAttribute(FXMLLoader.java:1290) at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220) at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744) at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441) at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409) at application.TestController.loadFXML(TestController.java:21) at application.TestController.<init>(TestController.java:15) at sun.reflect.GeneratedConstructorAccessor2.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at java.lang.Class.newInstance(Class.java:442) at sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:51) at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:927) at javafx.fxml.FXMLLoader$RootElement.processAttribute(FXMLLoader.java:1290) at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220) at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744) at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441) at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409) at application.TestController.loadFXML(TestController.java:21) at application.TestController.<init>(TestController.java:15) at sun.reflect.GeneratedConstructorAccessor2.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at java.lang.Class.newInstance(Class.java:442) at sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:51) at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:927) ・ ・ ・
###該当のソースコード
Test.java(メイン)
Java
1package application; 2 3import javafx.application.Application; 4import javafx.scene.Parent; 5import javafx.scene.Scene; 6import javafx.stage.Stage; 7 8 9public class Test extends Application { 10 11 private static Test instance; 12 private Stage stage; 13 14 @Override 15 public void start(Stage primaryStage) { 16 try { 17 instance = this; 18 stage = primaryStage; 19 sendTestController(); 20 stage.show(); 21 22 } catch(Exception e) { 23 e.printStackTrace(); 24 } 25 } 26 27 public static void main(String[] args) { 28 launch(args); 29 } 30 31 public void sendTestController() { 32 stage.setTitle("Test"); 33 TestController controller = new TestController(); 34 this.replaceSceneContent(controller); 35 } 36 public void replaceSceneContent(Parent controller) { 37 Scene scene = stage.getScene(); 38 if(scene == null) { 39 scene = new Scene(controller); 40 stage.setScene(scene); 41 } else { 42 stage.getScene().setRoot(controller); 43 } 44 } 45 46 public static Test getInstance() { 47 return instance; 48 } 49} 50 51
TestController.java(コントローラ)
Java
1package application; 2 3import java.io.IOException; 4import java.net.URL; 5import java.util.ResourceBundle; 6 7import javafx.fxml.FXMLLoader; 8import javafx.fxml.Initializable; 9import javafx.scene.layout.VBox; 10 11public class TestController extends VBox implements Initializable { 12 13 public TestController() { 14 System.out.println("AAAAA"); 15 loadFXML(); 16 } 17 public void loadFXML() { 18 FXMLLoader fxmlLoader = new FXMLLoader(Test.class.getResource("../fxml/test.fxml")); 19 fxmlLoader.setRoot(this); 20 try { 21 fxmlLoader.load(); 22 23 } catch(IOException e) { 24 throw new RuntimeException(e); 25 } 26 } 27 28 @Override 29 public void initialize(URL location, ResourceBundle resources) { 30 31 } 32 33} 34
test.fxml
fxml
1<?xml version="1.0" encoding="UTF-8"?> 2 3<?import javafx.scene.control.Label?> 4<?import javafx.scene.layout.VBox?> 5<?import javafx.scene.text.Font?> 6 7 8<fx:root alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" type="VBox" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.TestController"> 9 <children> 10 <Label alignment="CENTER" text="Test"> 11 <font> 12 <Font size="36.0" /> 13 </font> 14 </Label> 15 </children> 16</fx:root> 17
###補足情報(言語/FW/ツール等のバージョンなど)
Eclipse4.7.0, SceneBuilder2.0, Mac
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/10/27 06:48