我正在尝试将自定义单元格插入到列表视图中,我试图在此处查看类似的问题,但它们似乎仅处理字符串数据
我以以下指南为基础来尝试此https://www.turais.de/how-to-custom-listview-cell-in-javafx/ 无论我尝试什么,似乎都没有数据传递到updateItem,所以我一直收到此错误日志
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 un.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$1(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
at message.stick.ContactCellController.updateItem(ContactCellController.java:44)
at message.stick.ContactCellController.updateItem(ContactCellController.java:24)
at javafx.scene.control.ListCell.updateItem(ListCell.java:480)
at javafx.scene.control.ListCell.access$300(ListCell.java:72)
at javafx.scene.control.ListCell$4.invalidated(ListCell.java:299)
at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:111)
at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:146)
at javafx.scene.control.ListCell.setListView(ListCell.java:305)
at javafx.scene.control.ListCell.updateListView(ListCell.java:494)
at com.sun.javafx.scene.control.skin.ListViewSkin.createCell(ListViewSkin.java:292)
at com.sun.javafx.scene.control.skin.ListViewSkin.lambda$new$2(ListViewSkin.java:99)
at com.sun.javafx.scene.control.skin.VirtualFlow.getCell(VirtualFlow.java:1777)
at com.sun.javafx.scene.control.skin.VirtualFlow.getCellLength(VirtualFlow.java:1879)
at com.sun.javafx.scene.control.skin.VirtualFlow.computeViewportOffset(VirtualFlow.java:2528)
at com.sun.javafx.scene.control.skin.VirtualFlow.layoutChildren(VirtualFlow.java:1189)
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.Scene.doLayoutPass(Scene.java:552)
at javafx.scene.Scene.preferredSize(Scene.java:1646)
at javafx.scene.Scene.impl_preferredSize(Scene.java:1720)
at javafx.stage.Window$9.invalidated(Window.java:864)
at javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:109)
at javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:144)
at javafx.stage.Window.setShowing(Window.java:940)
at javafx.stage.Window.show(Window.java:955)
at javafx.stage.Stage.show(Stage.java:259)
at message.stick.MessageStick.start(MessageStick.java:31)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$null$10(GtkApplication.java:245)
... 1 more
以下是我的contactCellController和MainWindowController的代码的当前状态 ContactCellController
import eu.hansolo.tilesfx.addons.Indicator;
import java.io.IOException;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.layout.AnchorPane;
/**
* FXML Controller class
*
* @author james
*/
public class ContactCellController extends ListCell<Contacts>
{
@FXML
private Indicator statusIndicator;
@FXML
private Label usernameLabel;
@FXML
private Label statusMessageLabel;
private AnchorPane anchorPane;
private FXMLLoader mlLoader;
/**
* Initializes the controller class.
*/
@Override
protected void updateItem(Contacts contact, boolean empty)
{
System.out.println("boolean empty variable value: " + empty);
System.out.println("Contact Contents: " + contact.toString());
super.updateItem(contact, empty);
if (empty )//|| contact == null)
{
System.out.println("empty value is true");
setText(null);
setGraphic(null);
}
else
{
System.out.println("contact isn't null or empty is false");
if (mlLoader == null)
{
mlLoader = new FXMLLoader(getClass().getResource("contactCell.fxml"));
//mlLoader.setController(this);
try
{
mlLoader.load();
usernameLabel.setText(contact.getUsername());
statusMessageLabel.setText(contact.getStatusMessage());
}
catch (IOException ex)
{
System.out.println("Contact cell error in updateItem");
ex.printStackTrace();
}
}
else
{
setText(null);
setGraphic(anchorPane);
}
}
}
}
MainWindowController
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.fxml.Initializable;
import javafx.scene.control.ListView;
import javafx.fxml.FXML;
import javafx.scene.control.ListCell;
import javafx.util.Callback;
import javafx.collections.ObservableList;
/**
* FXML Controller class
*
* @author james
*/
public class MainWindowController implements Initializable
{
@FXML
private ListView myListView;
private ObservableList<Contacts> contactsObservableList = FXCollections.observableArrayList();
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb)
{
setupData();
myListView.setItems(contactsObservableList);
myListView.setCellFactory(new Callback<ListView<Contacts>, ListCell<Contacts>>()
{
@Override
public ListCell<Contacts> call(ListView<Contacts> contactsListView)
{
return new ContactCellController();
}
});
}
private void setupData()
{
contactsObservableList.addAll(
new Contacts("James", "Available",Contacts.Status.online),
new Contacts("Jimmy", "unavailable", Contacts.Status.offline));
System.out.println("contactsObservableList number of entries: " + contactsObservableList.size());
}
}
如果有人可以指出错误,那么我需要修复,并且可能是一个很棒的解决方案。
最佳问候 詹姆斯