有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JavaFX和MVP,对象在不应该被垃圾收集的时候被垃圾收集

我已经在这个问题上纠结了一段时间,我真的是在我最后的希望。我真的不知道问题出在哪里。我有一个GUI项目,使用我希望是正确的MVP形式,我的问题是我有一个presenter对象,当在任何方法中使用时,它在我的视图类中变为null。到目前为止,gui应该加载,用户应该点击按钮,该方法调用presenter对象的方法来执行某些操作,但它始终为空。我尝试传递到视图类的构造函数中,使其仅在视图类中成为对象,等等,但我无法修复它。感谢您的帮助。我不知道发生了什么,我认为它似乎对任何其他物体都没有这种作用

梅因。爪哇

package main;

import model.Model;
import presenter.Presenter;
import view.View;

public class Main
{
    public static void main(String[] args) 
    {
        View view = new View();
        Model model = new Model();
        Presenter presenter = new Presenter(view, model);
        view.setPresenter(presenter);
        // this was a way I found on stack overflow to call
        // another class that inhierts the application class
        // and be able to have args passed if needed
        View.launch(View.class, args);
    }

}

视图。爪哇

    package view;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import presenter.Presenter;

public class View extends Application
{
    private Presenter presenter;
    private Button test;
    private Views views; 

    public final static String LOGINVIEW = "loginscreen";
    public final static String LOGINVIEWFILE = "FXMLViewLogin.fxml";
    public final static String CHATVIEW = "chatscreen";
    public final static String CHATVIEWFILE = "FXMLViewChat.fxml";

    public View()
    {
        // init Views class
        views = new Views();
    }

    public void setPresenter(Presenter pres)
    {
        this.presenter = pres;
    }

    @Override
    public void start(Stage primaryStage) 
    {
        loadViews();
        primaryStage.setScene(setView(LOGINVIEW));
        primaryStage.setTitle("");
        primaryStage.show();
    }

    private void loadViews()
    {
        views.loadScreen(LOGINVIEW, LOGINVIEWFILE);
        views.loadScreen(CHATVIEW, CHATVIEWFILE);
    }

    private Scene setView(String name)
    {
        return views.setView(name);
    }

    @FXML
    private void test(ActionEvent event) 
    {
        //***********************************************************
        // ALWAYS NULL IN THIS EVENT METHOD METHOD AND ANY OTHER METHOD FOR THAT FACT AFTER setPresenter
        if(presenter == null)
            System.out.println("Why is this null right here?");
    }
}

主持人。爪哇

package presenter;

import view.View;
import model.Model;

public class Presenter 
{
    private Model model;
    private View view;

    public Presenter(View view, Model model)
    {
        this.model = model;
        this.view = view;

    }

    // any methods start here 
}

观点。爪哇

package view;

import java.util.HashMap;
import javafx.fxml.FXMLLoader;

import javafx.scene.Parent;
import javafx.scene.Scene;


public class Views 
{
    private final HashMap<String, Parent> view;

    public Views() 
    {
        this.view = new HashMap<>();
    }

    public void addView(String name, Parent screen) 
    {
        view.put(name, screen);
    }

    public boolean loadScreen(String name, String file)
    {
        try 
        {
            FXMLLoader myLoader = new FXMLLoader(getClass().getResource(file));
            Parent loadScreen = (Parent) myLoader.load();
            addView(name, loadScreen);
            return true;
        } 
        catch (Exception e) 
        {
            return false;
        }
    }

    public Scene setView(final String name) 
    {
        Scene scene = null;
        if (view.get(name) != null)  
            scene = new Scene(getParentNode(name));   
        unloadView(name);
        return scene;
    }

    private boolean unloadView(String name) 
    {
        return view.remove(name) != null;
    }

    private Parent getParentNode(String name)
    {
        return view.get(name);
    }

}

共 (0) 个答案