gpt4 book ai didi

java - 涉及 JavaFX 和泛型的误导性堆栈跟踪

转载 作者:行者123 更新时间:2023-11-29 07:37:52 29 4
gpt4 key购买 nike

在使用 Java 1.8u40 打开带有提示类型和附加的 StringConverter 的组合框时,我遇到了以下错误。这可以追溯到执行 FXML 的团队留下的示例字符串,与 Controller 中的通用类型 ComboBox 冲突。我们通过确保在 initialize() 中将虚拟列表替换为正确类型的项目来解决此问题。

然而,虽然我了解问题以及如何解决它,但我无法理解堆栈跟踪...错误发生在 Controller$1.toString(Controller.java:1)

  • 为什么会在第 1 行报告,Controller 本身不是通用的,只有它的字段...?
  • 是否涉及一些编译器生成的魔法?
  • FXML 构建过程如何构建正确类型的 ComboBox,因为泛型仅存在于编译类型中?

原始错误

Exception in thread "JavaFX Application Thread" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at Controller$1.toString(Controller.java:1)
at com.sun.javafx.scene.control.skin.ComboBoxListViewSkin.updateDisplayText(ComboBoxListViewSkin.java:388)
at com.sun.javafx.scene.control.skin.ComboBoxListViewSkin.access$100(ComboBoxListViewSkin.java:57)
at com.sun.javafx.scene.control.skin.ComboBoxListViewSkin$2$1.updateItem(ComboBoxListViewSkin.java:425)
at javafx.scene.control.ListCell.updateItem(ListCell.java:471)
at javafx.scene.control.ListCell.indexChanged(ListCell.java:330)
at javafx.scene.control.IndexedCell.updateIndex(IndexedCell.java:116)
at com.sun.javafx.scene.control.skin.VirtualFlow.setCellIndex(VirtualFlow.java:1957)
at com.sun.javafx.scene.control.skin.VirtualFlow.getCell(VirtualFlow.java:1797)
at com.sun.javafx.scene.control.skin.VirtualFlow.getCellBreadth(VirtualFlow.java:1888)
at com.sun.javafx.scene.control.skin.VirtualFlow.getMaxCellWidth(VirtualFlow.java:2508)
at com.sun.javafx.scene.control.skin.VirtualContainerBase.getMaxCellWidth(VirtualContainerBase.java:94)
at com.sun.javafx.scene.control.skin.ComboBoxListViewSkin$3.computePrefWidth(ComboBoxListViewSkin.java:457)
at javafx.scene.Parent.prefWidth(Parent.java:904)
at javafx.scene.layout.Region.prefWidth(Region.java:1419)
at com.sun.javafx.scene.control.skin.ComboBoxPopupControl.sizePopup(ComboBoxPopupControl.java:199)
at com.sun.javafx.scene.control.skin.ComboBoxPopupControl.positionAndShowPopup(ComboBoxPopupControl.java:173)
at com.sun.javafx.scene.control.skin.ComboBoxPopupControl.show(ComboBoxPopupControl.java:154)
at com.sun.javafx.scene.control.skin.ComboBoxBaseSkin.handleControlPropertyChanged(ComboBoxBaseSkin.java:127)
at com.sun.javafx.scene.control.skin.ComboBoxListViewSkin.handleControlPropertyChanged(ComboBoxListViewSkin.java:159)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase.lambda$registerChangeListener$61(BehaviorSkinBase.java:197)
at com.sun.javafx.scene.control.MultiplePropertyChangeListenerHandler$1.changed(MultiplePropertyChangeListenerHandler.java:55)
at javafx.beans.value.WeakChangeListener.changed(WeakChangeListener.java:89)
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.ReadOnlyBooleanWrapper$ReadOnlyPropertyImpl.fireValueChangedEvent(ReadOnlyBooleanWrapper.java:178)
at javafx.beans.property.ReadOnlyBooleanWrapper$ReadOnlyPropertyImpl.access$100(ReadOnlyBooleanWrapper.java:148)
at javafx.beans.property.ReadOnlyBooleanWrapper.fireValueChangedEvent(ReadOnlyBooleanWrapper.java:144)
at javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:110)
at javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:144)
at javafx.scene.control.ComboBoxBase.setShowing(ComboBoxBase.java:185)
at javafx.scene.control.ComboBoxBase.show(ComboBoxBase.java:391)
at com.sun.javafx.scene.control.behavior.ComboBoxBaseBehavior.show(ComboBoxBaseBehavior.java:242)
at com.sun.javafx.scene.control.behavior.ComboBoxBaseBehavior.mouseReleased(ComboBoxBaseBehavior.java:197)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:350)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$351(GlassViewEventHandler.java:385)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:384)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)

小例子

我已经使用 Integer 来表示其他一些业务特定类型,太复杂而无法粘贴到此处,值列表实际上是动态的并从数据库加载

FXML

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="205.0" prefWidth="168.0" styleClass="layout" xmlns:fx="http://javafx.com/fxml" fx:controller="Controller">
<children>
<ComboBox fx:id="combo" prefWidth="200" promptText="...">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="Foo" />
<String fx:value="Bar" />
<String fx:value="Baz" />
</FXCollections>
</items>
</ComboBox>
</children>
</AnchorPane>

Controller

public class Controller implements Initializable {
@FXML
private ComboBox<Integer> combo;
@Override
public void initialize(URL location, ResourceBundle resources) {
combo.setConverter(new StringConverter<Integer>() {
@Override
public String toString(Integer object) {
return "custom " + object.intValue();
}
@Override
public Integer fromString(String string) {
return null;
}
});
}
}

主要

public class ComboBoxTypeMismatch extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
try {
stage.setScene(new Scene(FXMLLoader.load(getClass().getResource("/test.fxml"))));
} catch (IOException e) {
e.printStackTrace();
}
stage.show();
}
}

最佳答案

虽然这个答案不是 OP 原始问题的解决方案,但我仍然将这个问题留给遇到相同堆栈跟踪并且不知道出了什么问题的人。


您正在声明 ComboBox<Integer>在您的 Controller 中,当您在 FXML 中将 String 值传递给它时,因此会发生异常。

java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer

如果你想要一个包含整数值的组合框,那么你需要将整数值传递给你的组合框。

 <FXCollections fx:factory="observableArrayList">
<Integer fx:value="1" />
<Integer fx:value="2" />
<Integer fx:value="3" />
</FXCollections>

关于java - 涉及 JavaFX 和泛型的误导性堆栈跟踪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33514931/

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