我有一个带有Textfield1和Button的View1。当我单击一个按钮时,我想转到视图2并在View2中将该值设置为Textfield2。当我做它时,它会打开一个新的场景。
这是传递值的代码:
charmListView.selectedItemProperty().addListener((obs, oldSelection,
newSelection) -> {
if (newSelection != null) {
ModellList l = new ModellList();
charmListView.selectedItemProperty().get().getName();
l.setName(charmListView.selectedItemProperty()
.get().getName());
SucheFahrtPresenter s = new SucheFahrtPresenter();
MobileApplication.getInstance()
.switchView(SUCHEFAHRT_VIEW);
s.setDP(l.getName());
}
});
我有这个错误:
Exception in thread "JavaFX Application Thread"
java.lang.NullPointerException
at com.gluonapplication.views.SucheFahrtPresenter.setDP(SucheFahrtPresenter.java:122)
at com.gluonapplication.views.BasicView$3.lambda$run$0(BasicView.java:119)
at com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:361)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
at javafx.beans.property.ObjectPropertyBase.fireValueChangedEvent(ObjectPropertyBase.java:105)
at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:112)
at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:146)
at com.gluonhq.charm.glisten.control.CharmListView.setSelectedItem(SourceFile:710)
我尝试了第二种解决方案:
那是第二位主持人:
import javax.inject.Inject;
@Inject
private Model model;
public void initialize() {
........
second.showingProperty().addListener((obs, oldValue, newValue) -> {
if (newValue) {
DP.setText(model.getText());
}
});
}
这是第一位主持人:
import javax.inject.Inject;
@Inject
private Model model;
@FXML
public void test() {
charmListView.selectedItemProperty().
addListener((obs, oldSelection, newSelection) -> {
if (newSelection != null) {
ModellList l = new ModellList();
charmListView.selectedItemProperty().get().getName();
l.setName(charmListView.selectedItemProperty().
get().getName());
SucheFahrtPresenter s = new SucheFahrtPresenter();
model.setText(l.getName());
MobileApplication.getInstance().switchView(SUCHEFAHRT_VIEW);
}
});
}
这些是我在Gradle中的依赖项:
dependencies {
compile 'com.gluonhq:charm:4.2.0'
compile group: 'org.controlsfx', name: 'controlsfx', version: '8.40.12'
compile 'de.jensd:fontawesomefx:8.9'
androidRuntime 'org.sqldroid:sqldroid:1.0.3'
androidCompile 'org.glassfish:javax.json:1.0.4'
compile group: 'org.xerial', name: 'sqlite-jdbc', version: '3.15.1'
compile group: 'org.sqldroid', name: 'sqldroid', version: '1.0.3'
compile group: 'com.gluonhq', name: 'charm-down-desktop', version:
'0.0.2'
compile group: 'com.gluonhq', name: 'charm-down-common', version:
'0.0.1'
compile group: 'com.gluonhq', name: 'charm-down-android', version:
'0.0.1'
compile group: 'com.airhacks', name: 'afterburner.fx', version: '1.7.0'
}
这是我的问题:
更改Charmlistview的代码后。我有这个错误
线程中的异常" JavaFX应用程序线程"显示java.lang.NullPointerException at com.gluonapplication.views.SucheFahrtPresenter.lambda $ setDP $ 4(SucheFahrtPresenter.java:138) at com.sun.javafx.binding.ExpressionHelper $ Generic.fireValueChangedEvent(ExpressionHelper.java:361) at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81) 在javafx.beans.property.ReadOnlyBooleanPropertyBase.fireValueChangedEvent(ReadOnlyBooleanPropertyBase.java:72) 在javafx.beans.property.ReadOnlyBooleanWrapper.fireValueChangedEvent(ReadOnlyBooleanWrapper.java:103) 在javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:110) 在javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:144) 在com.gluonhq.charm.glisten.mvc.View.c(SourceFile:1158)
答案 0 :(得分:2)
我建议您使用Gluon模板Project MultiView与FXML和Afterburner,或Glisten-Afterburner。它们中的任何一个都允许您使用注入并获得演示者的有效实例。
解决方案1. Glisten-Afterburner
使用Glisten-Afterburner默认模板,我添加了两个TextField控件,每个控件都有一个。
您唯一需要的是暴露一个方法来设置第二个演示者的值:
slugurl
在第一个视图中,您可以在按钮事件处理程序中切换视图并设置文本:
public class SecondaryPresenter extends GluonPresenter<YourApp> {
@FXML
private View secondary;
@FXML
private TextField textField;
public void initialize() {
...
}
public void setText(String value) {
textField.setText(value);
}
}
此解决方案需要Gluon IDE插件2.5.0
解决方案2. MultiView
或者,您可以将MultiView与FXML和Afterburner模板一起使用,并创建一个Model类:
public class PrimaryPresenter extends GluonPresenter<YourApp> {
@FXML
private View primary;
@FXML
private CharmListView<String, String> charmListView;
public void initialize() {
charmListView.setItems(FXCollections.observableArrayList("One", "Two", "Three", "Four"));
charmListView.selectedItemProperty().addListener((obs, ov, nv) -> {
AppViewManager.SECONDARY_VIEW.switchView()
.ifPresent(presenter ->
((SecondaryPresenter) presenter).setText(nv));
});
...
}
}
您可以在两位演示者中注入:
public class Model {
private final StringProperty text = new SimpleStringProperty();
public final void setText(String value) { text.set(value); }
public final String getText() { return text.get(); }
public final StringProperty textProperty() { return text; }
}
和
public class PrimaryPresenter {
@FXML
private View primary;
@FXML
private CharmListView<String, String> charmListView;
@Inject
private Model model;
public void initialize() {
charmListView.setItems(FXCollections.observableArrayList("One", "Two", "Three", "Four"));
charmListView.selectedItemProperty().addListener((obs, ov, nv) -> {
model.setText(nv);
MobileApplication.getInstance().switchView(SECONDARY_VIEW);
});
...
}
}