- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我刚刚向 TreeTableView 添加了过滤功能,然后开始出现一个似乎是随机的(可能不是随机的)错误。我可以通过向过滤器文本字段发送垃圾邮件来重现它,以更新 TreeTableView 直到它堵塞。我得到以下堆栈跟踪,TreeTableView 不再更新,但程序不会崩溃。堆栈跟踪中与我的代码相关的唯一部分指向一个 cellFactory,我在其中重写 CheckBoxTreeTableCell 的 updateItem 以实现顶级行的样式:
colActShop.setCellFactory(column ->{
return new CheckBoxTreeTableCell<Product, Boolean>(){
@Override
public void updateItem(Boolean item, boolean empty) {
super.updateItem(item, empty);
boolean isTopLevel = getTreeTableView().getRoot().getChildren()
.contains(getTreeTableRow().getTreeItem());
if(item == null || empty){
setText(null);
setGraphic(null);
//It crashes on the following line according to the stack trace
getTreeTableRow().getStyleClass().remove("topLevelRow");
}else{
if(isTopLevel){
getTreeTableRow().getStyleClass().add("topLevelRow");
}else{
getTreeTableRow().getStyleClass().remove("topLevelRow");
}
setEditable(!isTopLevel);
}
}
};
});
如果我删除 getTreeTableRow().getStyleClass()...
行,它似乎会删除错误,但随后我会丢失我正在寻找的样式行为。我也拼命地尝试在某些地方放置一些 Platform.runlater
但没有成功。我现在无法提供最小的可执行示例,因为我的过滤和 TreeTableView 更新变得非常大。当我在过滤器 textField 中输入文本时,将调用以下函数来更新 TreeTableView 的根:
/**
* A method to update a TreeTableView every time backing data changes in the model,
* without resetting the table by only adding and removing data when required.
* Also applies filters
* @param root : the root TreeItem of the table to update
* @param inventory : the inventory backing this table
* @param shop : the shop to calculate the price of products
* @param filter : comma separated string filter
* @param catFilter : category filter list
*/
private void updateTreeItemRoot(TreeItem<Product> root, Inventory inventory, Shop shop, String filter, ArrayList<String> catFilter){
inventory.clean();
shop.clean();
inventory.calculateAverageData();
//retrieve the product list of category items of the previous TreeTableView structure before updating
ArrayList<Product> oldProdCategoryList= new ArrayList<Product>();
ArrayList<TreeItem<Product>> oldTreeItemCategoryList= new ArrayList<>(root.getChildren());
for(TreeItem<Product> treeItem : oldTreeItemCategoryList){
oldProdCategoryList.add(treeItem.getValue());
}
for(Entry<String, ArrayList<Product>> entry : inventory.getData().entrySet()){
Product product = inventory.getAverageData(entry.getKey());
//Filtering the TreeTableView
boolean filterOK = false;
if(catFilter!=null){
if(catFilter.contains("All")||catFilter.contains("All seeds")){
filterOK = true;
}else{
//else look for any matching categories
for(String str:catFilter){
if(product.getCategories().stream().anyMatch(s -> s.equals(str))){
filterOK = true;
break;
}
}
}
//turn filterOk to false if we want only mature and product is not
if(catFilter.contains("Only mature")&&product.getMaturity()!=100) {
filterOK = false;
}
//turn filterOK to false if we want only seeds and the product is not a seed
if(catFilter.contains("Only seeds")){
if(!(product.getCategories().stream().anyMatch(s -> s.equals("Seed")))){
filterOK = false;
}
}
}else{
filterOK = true;
}
String[] filterList = filter.split(",");
if(filter.length()>0&&!Arrays.stream(filterList).anyMatch(s->product.getName().matches( "(?i:.*"+s+".*)"))){
filterOK = false;
}
if(filterOK){
product.updatePrice(shop);
TreeItem<Product> averageProdDataItem; ;
//if this product category is not yet present in the table add it.
if(!oldProdCategoryList.contains(product)){
averageProdDataItem = new TreeItem<Product>(product);
averageProdDataItem.setExpanded(true);
root.getChildren().add(averageProdDataItem);
}else{
//else if this product category is already in the table, retrieve the reference and remove it from the
// "old" Lists to see if something needs to be removed from the table in the end
averageProdDataItem = oldTreeItemCategoryList.get(oldProdCategoryList.indexOf(product));
oldTreeItemCategoryList.remove(oldProdCategoryList.indexOf(product));
oldProdCategoryList.remove(product);
}
//retrieve the product list for this category category of the previous TreeTableView structure before updating
ArrayList<Product> oldProdList= new ArrayList<Product>();
ArrayList<TreeItem<Product>> oldTreeItemList= new ArrayList<>(averageProdDataItem.getChildren());
for(TreeItem<Product> treeItem : oldTreeItemList){
oldProdList.add(treeItem.getValue());
}
for(Product prod : entry.getValue()){
prod.updatePrice(shop);
TreeItem<Product> treeItem;
//if the table does not contain this prod, add it
if(!oldProdList.contains(prod)){
treeItem = new TreeItem<Product>(prod);
averageProdDataItem.getChildren().add(treeItem);
}else{
oldTreeItemList.remove(oldProdList.indexOf(prod));
oldProdList.remove(prod);
}
}
averageProdDataItem.getChildren().removeAll(oldTreeItemList); //clean up empty products
}// if filterOK
}//for
root.getChildren().removeAll(oldTreeItemCategoryList);// clean up empty categories
}
好像和this issue一样已打开 3 次并关闭,没有任何评论。
堆栈跟踪:
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at com.sun.javafx.scene.control.skin.CellSkinBase.access$200(CellSkinBase.java:54)
at com.sun.javafx.scene.control.skin.CellSkinBase$StyleableProperties$1.getStyleableProperty(CellSkinBase.java:149)
at com.sun.javafx.scene.control.skin.CellSkinBase$StyleableProperties$1.getStyleableProperty(CellSkinBase.java:138)
at javafx.scene.CssStyleHelper.resetToInitialValues(CssStyleHelper.java:447)
at javafx.scene.CssStyleHelper.createStyleHelper(CssStyleHelper.java:180)
at javafx.scene.Node.reapplyCss(Node.java:8983)
at javafx.scene.Node.reapplyCss(Node.java:9012)
at javafx.scene.Node.impl_reapplyCSS(Node.java:8946)
at javafx.scene.Node$3.onChanged(Node.java:1023)
at com.sun.javafx.collections.TrackableObservableList.lambda$new$29(TrackableObservableList.java:45)
at com.sun.javafx.collections.ListListenerHelper$SingleChange.fireValueChangedEvent(ListListenerHelper.java:164)
at com.sun.javafx.collections.ListListenerHelper.fireValueChangedEvent(ListListenerHelper.java:73)
at javafx.collections.ObservableListBase.fireChange(ObservableListBase.java:233)
at javafx.collections.ListChangeBuilder.commit(ListChangeBuilder.java:482)
at javafx.collections.ListChangeBuilder.endChange(ListChangeBuilder.java:541)
at javafx.collections.ObservableListBase.endChange(ObservableListBase.java:205)
at javafx.collections.ModifiableObservableListBase.remove(ModifiableObservableListBase.java:183)
at javafx.collections.ModifiableObservableListBase.remove(ModifiableObservableListBase.java:171)
at homier.farmGame.controller.Engine$2.updateItem(Engine.java:768)
at homier.farmGame.controller.Engine$2.updateItem(Engine.java:1)
at javafx.scene.control.TreeTableCell.updateItem(TreeTableCell.java:630)
at javafx.scene.control.TreeTableCell.indexChanged(TreeTableCell.java:457)
at javafx.scene.control.IndexedCell.updateIndex(IndexedCell.java:116)
at com.sun.javafx.scene.control.skin.TableRowSkinBase.recreateCells(TableRowSkinBase.java:671)
at com.sun.javafx.scene.control.skin.TableRowSkinBase.updateCells(TableRowSkinBase.java:499)
at com.sun.javafx.scene.control.skin.TreeTableRowSkin.updateCells(TreeTableRowSkin.java:220)
at com.sun.javafx.scene.control.skin.TableRowSkinBase.checkState(TableRowSkinBase.java:631)
at com.sun.javafx.scene.control.skin.TableRowSkinBase.computePrefHeight(TableRowSkinBase.java:571)
at javafx.scene.control.Control.computePrefHeight(Control.java:547)
at javafx.scene.Parent.prefHeight(Parent.java:935)
at javafx.scene.layout.Region.prefHeight(Region.java:1435)
at com.sun.javafx.scene.control.skin.VirtualFlow.resizeCellSize(VirtualFlow.java:1947)
at com.sun.javafx.scene.control.skin.VirtualFlow.addLeadingCells(VirtualFlow.java:1247)
at com.sun.javafx.scene.control.skin.VirtualFlow.layoutChildren(VirtualFlow.java:1194)
at javafx.scene.Parent.layout(Parent.java:1087)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Scene.doLayoutPass(Scene.java:552)
at javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2397)
at com.sun.javafx.tk.Toolkit.lambda$runPulse$29(Toolkit.java:398)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:397)
at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:424)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:518)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:498)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulseFromQueue(QuantumToolkit.java:491)
at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$403(QuantumToolkit.java:319)
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$147(WinApplication.java:177)
at java.lang.Thread.run(Unknown Source)
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at com.sun.javafx.scene.control.skin.CellSkinBase.access$200(CellSkinBase.java:54)
at com.sun.javafx.scene.control.skin.CellSkinBase$StyleableProperties$1.getStyleableProperty(CellSkinBase.java:149)
at com.sun.javafx.scene.control.skin.CellSkinBase$StyleableProperties$1.getStyleableProperty(CellSkinBase.java:138)
at javafx.scene.CssStyleHelper.resetToInitialValues(CssStyleHelper.java:447)
at javafx.scene.CssStyleHelper.createStyleHelper(CssStyleHelper.java:180)
at javafx.scene.Node.reapplyCss(Node.java:8983)
at javafx.scene.Node.impl_reapplyCSS(Node.java:8946)
at javafx.scene.control.Control$1.invalidated(Control.java:300)
at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:111)
at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:146)
at javafx.css.StyleableObjectProperty.set(StyleableObjectProperty.java:82)
at javafx.scene.control.Control$1.set(Control.java:237)
at javafx.scene.control.Control$1.set(Control.java:220)
at javafx.scene.control.Control.setSkin(Control.java:217)
at com.sun.javafx.scene.control.skin.TableRowSkinBase.recreateCells(TableRowSkinBase.java:673)
at com.sun.javafx.scene.control.skin.TableRowSkinBase.updateCells(TableRowSkinBase.java:499)
at com.sun.javafx.scene.control.skin.TreeTableRowSkin.updateCells(TreeTableRowSkin.java:220)
at com.sun.javafx.scene.control.skin.TableRowSkinBase.checkState(TableRowSkinBase.java:631)
at com.sun.javafx.scene.control.skin.TableRowSkinBase.computePrefHeight(TableRowSkinBase.java:571)
at javafx.scene.control.Control.computePrefHeight(Control.java:547)
at javafx.scene.Parent.prefHeight(Parent.java:935)
at javafx.scene.layout.Region.prefHeight(Region.java:1435)
at com.sun.javafx.scene.control.skin.VirtualFlow.resizeCellSize(VirtualFlow.java:1947)
at com.sun.javafx.scene.control.skin.VirtualFlow.addLeadingCells(VirtualFlow.java:1247)
at com.sun.javafx.scene.control.skin.VirtualFlow.layoutChildren(VirtualFlow.java:1194)
at javafx.scene.Parent.layout(Parent.java:1087)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Scene.doLayoutPass(Scene.java:552)
at javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2397)
at com.sun.javafx.tk.Toolkit.lambda$runPulse$29(Toolkit.java:398)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:397)
at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:424)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:518)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:498)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulseFromQueue(QuantumToolkit.java:491)
at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$403(QuantumToolkit.java:319)
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$147(WinApplication.java:177)
at java.lang.Thread.run(Unknown Source)
有人有指导我调试的想法吗?我需要放弃某些行的样式吗?
编辑 1经过进一步的测试后,它的随机性比我想象的要小。我在过滤器字段中输入一个字母,并在卡住之前将其删除 34 次,可重现。如果我使用一系列字母(而不是每次使用相同的字母),它似乎会早于 34,例如 29 或 31。所以这让我觉得每次迭代都没有清理掉某些东西,但我不知道是什么。
最佳答案
我仍然不知道该错误的原因。我想我找到了一种解决方法,通过在 TreeTableView 上使用 setRowFactory
而不是在列上使用 setCellFactory
和 getTreeTableRow()
来设置特殊行的样式。这看起来很奇怪,因为 Row 是同一个对象,我只是以不同的方式访问它,但到目前为止我还没有用这种方式遇到错误。
tableShop.setRowFactory(table-> {
return new TreeTableRow<Product>(){
@Override
public void updateItem(Product pers, boolean empty) {
super.updateItem(pers, empty);
boolean isTopLevel = table.getRoot().getChildren().contains(treeItemProperty().get());
if(pers==null||empty) {
setText(null);
setGraphic(null);
getStyleClass().remove("topLevelRow");
}else {
if(isTopLevel) {
if(!getStyleClass().contains("topLevelRow")) {
getStyleClass().add("topLevelRow");
}
}else {
getStyleClass().remove("topLevelRow");
}
}
}
};
});
关于java - 更新 TreeTableView 时看似随机的 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53365831/
我正在编写一个具有以下签名的 Java 方法。 void Logger(Method method, Object[] args); 如果一个方法(例如 ABC() )调用此方法 Logger,它应该
我是 Java 新手。 我的问题是我的 Java 程序找不到我试图用作的图像文件一个 JButton。 (目前这段代码什么也没做,因为我只是得到了想要的外观第一的)。这是我的主课 代码: packag
好的,今天我在接受采访,我已经编写 Java 代码多年了。采访中说“Java 垃圾收集是一个棘手的问题,我有几个 friend 一直在努力弄清楚。你在这方面做得怎么样?”。她是想骗我吗?还是我的一生都
我的 friend 给了我一个谜语让我解开。它是这样的: There are 100 people. Each one of them, in his turn, does the following
如果我将使用 Java 5 代码的应用程序编译成字节码,生成的 .class 文件是否能够在 Java 1.4 下运行? 如果后者可以工作并且我正在尝试在我的 Java 1.4 应用程序中使用 Jav
有关于why Java doesn't support unsigned types的问题以及一些关于处理无符号类型的问题。我做了一些搜索,似乎 Scala 也不支持无符号数据类型。限制是Java和S
我只是想知道在一个 java 版本中生成的字节码是否可以在其他 java 版本上运行 最佳答案 通常,字节码无需修改即可在 较新 版本的 Java 上运行。它不会在旧版本上运行,除非您使用特殊参数 (
我有一个关于在命令提示符下执行 java 程序的基本问题。 在某些机器上我们需要指定 -cp 。 (类路径)同时执行java程序 (test为java文件名与.class文件存在于同一目录下) jav
我已经阅读 StackOverflow 有一段时间了,现在我才鼓起勇气提出问题。我今年 20 岁,目前在我的家乡(罗马尼亚克卢日-纳波卡)就读 IT 大学。足以介绍:D。 基本上,我有一家提供簿记应用
我有 public JSONObject parseXML(String xml) { JSONObject jsonObject = XML.toJSONObject(xml); r
我已经在 Java 中实现了带有动态类型的简单解释语言。不幸的是我遇到了以下问题。测试时如下代码: def main() { def ks = Map[[1, 2]].keySet()
一直提示输入 1 到 10 的数字 - 结果应将 st、rd、th 和 nd 添加到数字中。编写一个程序,提示用户输入 1 到 10 之间的任意整数,然后以序数形式显示该整数并附加后缀。 public
我有这个 DownloadFile.java 并按预期下载该文件: import java.io.*; import java.net.URL; public class DownloadFile {
我想在 GUI 上添加延迟。我放置了 2 个 for 循环,然后重新绘制了一个标签,但这 2 个 for 循环一个接一个地执行,并且标签被重新绘制到最后一个。 我能做什么? for(int i=0;
我正在对对象 Student 的列表项进行一些测试,但是我更喜欢在 java 类对象中创建硬编码列表,然后从那里提取数据,而不是连接到数据库并在结果集中选择记录。然而,自从我这样做以来已经很长时间了,
我知道对象创建分为三个部分: 声明 实例化 初始化 classA{} classB extends classA{} classA obj = new classB(1,1); 实例化 它必须使用
我有兴趣使用 GPRS 构建车辆跟踪系统。但是,我有一些问题要问以前做过此操作的人: GPRS 是最好的技术吗?人们意识到任何问题吗? 我计划使用 Java/Java EE - 有更好的技术吗? 如果
我可以通过递归方法反转数组,例如:数组={1,2,3,4,5} 数组结果={5,4,3,2,1}但我的结果是相同的数组,我不知道为什么,请帮助我。 public class Recursion { p
有这样的标准方式吗? 包括 Java源代码-测试代码- Ant 或 Maven联合单元持续集成(可能是巡航控制)ClearCase 版本控制工具部署到应用服务器 最后我希望有一个自动构建和集成环境。
我什至不知道这是否可能,我非常怀疑它是否可能,但如果可以,您能告诉我怎么做吗?我只是想知道如何从打印机打印一些文本。 有什么想法吗? 最佳答案 这里有更简单的事情。 import javax.swin
我是一名优秀的程序员,十分优秀!