【问题标题】:local variables referenced from an inner class must be final or effectively final 3从内部类引用的局部变量必须是 final 或实际上是 final 3
【发布时间】:2016-08-27 21:00:35
【问题描述】:

我收到错误消息:“从内部类引用的局部变量必须是最终的或有效的最终”

这个错误指的是:

MyTreeItem LeftRootItem = (MyTreeItem) treeViewLeft.getRoot();

我的错误是什么。为什么这个错误与“treeViewRight”不相似?

addAttribute.setOnAction(new EventHandler<ActionEvent>(){
    @Override
    public void handle(ActionEvent event) {
        TreeView treeViewLeft = new TreeView ();
        treeViewLeft.setShowRoot(false);
        treeViewLeft.setRoot(new MyTreeItem ("root"));
        treeViewLeft.getRoot().setExpanded(true);
        TreeView treeViewRight;
        Button button = new Button ("<<");
        HBox hBox = new HBox ();
        DBConnect.dbconnect();
        treeViewRight = DB.loadAttributeClasses();
        treeViewLeft = DB.getAttributesfromClassorProduct(selectedItem.getClassID());
        treeViewRight.setShowRoot(false);
        treeViewRight.getRoot().setExpanded(true);
        hBox.getChildren().addAll(treeViewLeft, button, treeViewRight);
        Scene scene = new Scene(hBox);

        Stage attributeSelect = new Stage ();
        attributeSelect.setTitle("Hello World!");
        attributeSelect.setScene(scene);
        attributeSelect.show();     

        button.setOnAction(new EventHandler<ActionEvent>(){

            @Override
            public void handle(ActionEvent event) {
                MyTreeItem rightItem = (MyTreeItem) treeViewRight.getSelectionModel().getSelectedItem();
                DB.setProdClassAttr(selectedItem.getClassID(), rightItem.getClassID(), selectedItem.getType());

                rightItem.getParent().getChildren().remove(rightItem);
                MyTreeItem LeftRootItem = (MyTreeItem) treeViewLeft.getRoot();
            }
        });
    }
});

【问题讨论】:

    标签: java javafx


    【解决方案1】:

    问题是您将两个不同的TreeViews 分配给treeViewLeft

    你的第四行是

    TreeView treeViewLeft = new TreeView ();
    

    再往下几行,您将新值分配给treeViewLeft

    treeViewLeft = DB.getAttributesfromClassorProduct(selectedItem.getClassID());
    

    如果您想从按钮 onAction 处理程序中引用 treeViewLeft,则不允许这样做。

    您只能为treeViewRight 赋值一次,这是允许的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-24
      • 2021-01-12
      • 1970-01-01
      • 2023-02-19
      • 1970-01-01
      • 2015-01-25
      相关资源
      最近更新 更多