JavaFX绑定失败"无法绑定到无类型对象。"

时间:2016-03-29 19:35:58

标签: java javafx java-8

我有一个定义JavaFX用户界面的FXML文件。在FXML文件中,我有以下内容:

...
<Checkbox fx:id="myCheckbox" ... />
<Spinner disable="${myCheckbox.selected}" ... />
...

当我尝试使用以下代码加载我的FXML文件时,我收到一条带有消息的javafx.fxml.LoadException

  

无法绑定到无类型对象。

final Class<?> controllerClass = ...;
final FXMLLoader fxmlLoader = new FXMLLoader();
final String fxmlPath = controllerClass.getSimpleName() + ".fxml";
try (final InputStream fxmlStream = controllerClass.getResourceAsStream(fxmlPath)) {
    fxmlLoader.load(fxmlStream);
    final Object controller = fxmlLoader.<Object>getController();
    return controller
}

当选中复选框时,是否有人知道如何修改FXML以禁用我的微调器?我知道如何在代码中进行此操作,但想要了解有关FXML语法的更多信息。我还应该补充一点,当我从FXML中删除尝试的属性绑定时,所有内容都按预期加载。感谢。

修改

由于@Groostav的评论说它必须是代码...部分的内容,我想出了一个简单,可重现的例子。在我的例子中,有两个文件,我将在这里完整复制。

Main.java:

package test;

import java.io.InputStream;

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

public class Main extends Application {
    public static void main(final String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage window) throws Exception {
        final FXMLLoader fxmlLoader = new FXMLLoader();
        final String fxmlPath = "Example.fxml";
        final Parent root;
        try (final InputStream fxmlStream = getClass().getResourceAsStream(fxmlPath)) {
            root = (Parent)fxmlLoader.load(fxmlStream);
        }
        final Scene scene = new Scene(root);
        window.setScene(scene);
        window.show();
    }
}

Example.fxml:

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

<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.Spinner?>
<?import javafx.scene.layout.HBox?>

<HBox xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1"
      alignment="BASELINE_LEFT" spacing="15">
  <children>
    <CheckBox fx:id="myCheckbox" text="Disable?" mnemonicParsing="false" />
    <Spinner disable="${myCheckbox.selected}" />
  </children>
</HBox>

当我运行main方法时,我得到以下输出:

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:745)
Caused by: javafx.fxml.LoadException: Cannot bind to untyped object.
unknown path:12

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597)
    at javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:103)
    at javafx.fxml.FXMLLoader$Element.processPropertyAttribute(FXMLLoader.java:299)
    at javafx.fxml.FXMLLoader$Element.processInstancePropertyAttributes(FXMLLoader.java:235)
    at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:749)
    at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2425)
    at test.Main.start(Main.java:22)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    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$148(WinApplication.java:191)
    ... 1 more
Exception running application test.Main

如果我从FXML文件中删除disable="${myCheckbox.selected}",则所有内容都按预期加载。另外,我能够在代码中绑定属性。为什么这不起作用,我如何修改FXML以支持这一点?我在Windows 7 Enterprise SP1 x64上运行,使用Java SE JDK 8u92 x64。

1 个答案:

答案 0 :(得分:0)

似乎你不能用这种方式使用FXML在Spinner上设置disabled属性。它在TextFields上运行正常。这可能是一个错误,我想这与加载器有关,期望定义ValueFactory的Spinner标签的主体,因此当执行将CheckBox状态链接到disable属性的尝试时,Spinner没有正确定义。

以下解决方法可能有所帮助:

let flowLayout = CSStickyHeaderFlowLayout()
    collectionView = UICollectionView(frame: CGRect(x: 0, y: self.navigationController!.navigationBar.frame.size.height+statusBarHeight() ,width: screenWidth,  height: screenHeight-(self.navigationController!.navigationBar.frame.size.height+statusBarHeight())), collectionViewLayout: flowLayout)    

self.collectionView?.register(CollectionParallaxHeader.self, forSupplementaryViewOfKind: CSStickyHeaderParallaxHeader, withReuseIdentifier: "parallaxHeader")

self.headerNib = UINib(nibName: "CSParallaxHeader", bundle: nil)
self.collectionView.register(self.headerNib, forSupplementaryViewOfKind: CSStickyHeaderParallaxHeader, withReuseIdentifier: "header")

fileprivate var layout : CSStickyHeaderFlowLayout? {
    return self.collectionView?.collectionViewLayout as? CSStickyHeaderFlowLayout
}

func onParallaxHeadered(_ count:Int)
{
    if count == 0
    {
        self.layout?.parallaxHeaderReferenceSize = CGSize(width: getScreenWidth(), height: 0)
    }
}

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    let view = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "parallaxHeader", for: indexPath) as! CollectionParallaxHeader

    return view
}