我正在用gluon mobile创建一个MultiView项目,当我切换到之前已经使用过的View
时,就会出现问题。
此特定视图是我的HOME_VIEW
,因此在应用程序首次启动时被称为:
带有视图声明的“ Main”类:
public class GluonApplication extends MobileApplication {
public static final String START_VIEW = HOME_VIEW;
public static final String LOGIN_VIEW = "Login View";
//other views
@Override
public void init() {
addViewFactory(START_VIEW, () -> new StartView());
addViewFactory(LOGIN_VIEW, () -> new LoginView());
//other views
DrawerManager.buildDrawer(this);
//other code
}
类StartView
。视图的内容是根据变量设置的,但是在这种情况下,只有else{}
包围的部分很重要。
public class StartView extends View {
public StartView() {
getStylesheets().add(StartView.class.getResource("start.css").toExternalForm());
LoginSpace lspace = new LoginSpace();
setCenter(lspace);
}
@Override
protected void updateAppBar(AppBar appBar) {
appBar.setNavIcon(MaterialDesignIcon.MENU.button(e -> getApplication().getDrawer().open()));
appBar.setTitleText("Login");
appBar.setDisable(true);
appBar.setVisible(false);
}
class LoginSpace extends VBox {
TextField name;
TextField pass;
public LoginSpace() {
g.p("set LoginSpace",2);
String s = null;
if(KopfrechenTrainer.getLoggedIn()) {
//code
}
else {
HBox h = new HBox(10.0);
name = new TextField();
pass = new TextField();
Button login = new Button("Anmelden");
Button reg = new Button("Neu registrieren");
name.setFloatText("Nutzername");
pass.setFloatText("Passwort:");
login.setOnAction(e -> onLogin());
reg.setOnAction(e -> onReg());
h.getChildren().addAll(login, reg);
h.setAlignment(Pos.CENTER);
getChildren().addAll(name, pass, h);
KopfrechenTrainer.initKT();
setSpacing(25);
setAlignment(Pos.CENTER);
Insets pad = new Insets(20,50,20,50);
setPadding(pad);
}
}
private void onContinue() {
//code
}
private void onLogin() {
g.p("button_login pressed",2);
String name = this.name.getText();
String pass = this.pass.getText();
boolean logIn = KopfrechenTrainer.checkLogin(name, pass);
if (logIn) {
this.name.setText(null);
this.pass.setText(null);
MobileApplication.getInstance().switchView("Login View");
}
else {
Alert fault = new Alert(AlertType.NONE, "Nutzername oder Passwort ist nicht korrekt. " +
"Bitte versuche es erneut oder registriere dich neu.");
fault.showAndWait();
}
}
START_VIEW
中未列出NavigationDrawer
,我不知道这是否重要。通常,LOGIN_VIEW
是从START_VIEW
调用的:
import static com.gluonapplication.GluonApplication.START_VIEW;
//other imports
public class LoginView extends View {
public LoginView() {
//...
HomeSpace hspace = new HomeSpace();
setCenter(hspace);
}
@Override
protected void updateAppBar(AppBar appBar) {
appBar.setNavIcon(MaterialDesignIcon.MENU.button(e -> getApplication().getDrawer().open()));
appBar.setTitleText("Startseite");
appBar.setDisable(false);
}
class HomeSpace extends VBox {
public HomeSpace(){
g.p("set HomeSpace", 2);
Label welcome = new Label("Herzlich wilkommen "+KopfrechenTrainer.getName()+"!");
Button logout = new Button("Abmelden");
Button leave = new Button("Abmelden und beenden");
Button quit = new Button("Beenden");
logout.setOnAction(e -> onLogout());
leave.setOnAction(e -> onLeave());
quit.setOnAction(e -> onQuit());
getChildren().addAll(welcome, logout, quit, leave);
setSpacing(25);
setAlignment(Pos.CENTER);
setVisible(true);
}
private void onLogout() {
KopfrechenTrainer.logout();
MobileApplication.getInstance().switchView(START_VIEW);//this line is of importance!
}
private void onLeave() {
//code
}
private void onQuit() {
//code
}
}
}
按下按钮注销后,视图切换到START_VIEW
,但同时调试了以下错误:
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at com.gluonhq.impl.charm.a.b.b.z.layoutChildren(SourceFile:159)
at com.gluonhq.impl.charm.a.b.b.y.layoutChildren(SourceFile:23)
at javafx.scene.control.Control.layoutChildren(Control.java:578)
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.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(Thread.java:748)
在新的START_VIEW上执行任何按钮操作都会失败,并且只会显示白屏。
我不知道为什么会这样,而且我已经建立了一个类似的测试应用程序,可以完美运行!预先感谢您提供解决该问题的任何想法!