首页 > 解决方案 > switchView 到前一个 View 导致 NullPointerException

问题描述

我正在使用 gluon mobile 创建一个 MultiView 项目,当我切换到View我之前已经使用过的项目时会出现问题。

这个特定的视图是 my HOME_VIEW,所以在应用程序启动时第一次调用它:

带有视图声明的“主”类:

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。View 的内容是根据变量设置的,但在这种情况下,只有被包围的部分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 上的任何按钮操作都会失败,并且只显示一个白屏。

我不知道为什么会发生这种情况,而且我已经构建了一个类似的测试应用程序,它可以完美运行!在此先感谢您提供如何解决该问题的任何想法!

标签: javajavafxnullpointerexceptiongluon-mobile

解决方案


推荐阅读