Codenameone SpanButton客户端属性引发NullPointerException
单击时,我试图在一个按钮上获取客户端属性。它正在抛出NullPointerException 我用一个普通的按钮测试了相同的代码,它运行得很好。我相信那里可能有一只虫子 以下是如何从裸体项目中重新创建此问题:Codenameone SpanButton客户端属性引发NullPointerException,codenameone,Codenameone,单击时,我试图在一个按钮上获取客户端属性。它正在抛出NullPointerException 我用一个普通的按钮测试了相同的代码,它运行得很好。我相信那里可能有一只虫子 以下是如何从裸体项目中重新创建此问题: Form hi = new Form("Hi World"); Button button = new Button("Button"); button.putClientProperty("id", 100); SpanButton spanButton
Form hi = new Form("Hi World");
Button button = new Button("Button");
button.putClientProperty("id", 100);
SpanButton spanButton = new SpanButton("SpanButton");
spanButton.putClientProperty("id", 200);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
int id = (int) evt.getComponent().getClientProperty("id");
System.out.println("Standard Button action listener: id = " + id);
}
});
spanButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
int id = (int) evt.getComponent().getClientProperty("id");
System.out.println("Span button action listener: id = " + id);
}
});
hi.addComponent(button);
hi.addComponent(spanButton);
hi.show();
如果单击该按钮,输出将正确打印:
标准按钮操作侦听器:id=100
如果单击span按钮,将引发NullPointerException。经过调查,我发现SpanButton getClientProperty(“id”)返回null
注意:我需要使用SpanButton,因为它支持可变大小 使用恰当命名的
getActualComponent()
而不是getComponent()
报告解释了原因:
与ActionEvent#getComponent()
相同,但如果潜在客户组件可用,则将返回该潜在客户组件
这对于将返回底层按钮的组件(如MultiButton
)非常重要