Java me J2me方法的同步

Java me J2me方法的同步,java-me,synchronize,Java Me,Synchronize,我在startApp方法中初始化一个变量,然后调用一个线程来检查应用程序的整个运行时间当前屏幕(画布)是什么,并激活它的线程 import java.io.IOException; import java.io.InputStream; import zuma.core.MyGameCanvas; import zuma.core.Game; import zuma.gui.LevelSelectionMenu; import zuma.gui.MainMenu; import javax.mi

我在startApp方法中初始化一个变量,然后调用一个线程来检查应用程序的整个运行时间当前屏幕(画布)是什么,并激活它的线程

import java.io.IOException;
import java.io.InputStream;
import zuma.core.MyGameCanvas;
import zuma.core.Game;
import zuma.gui.LevelSelectionMenu;
import zuma.gui.MainMenu;
import javax.microedition.lcdui.Display;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.midlet.*;
import zuma.util.AudioManager;

public class Midlet extends MIDlet implements Runnable{

    private static Display display;
    private static Game game;
    private static MainMenu mainMenu;
    private static LevelSelectionMenu levelSelectionMenu;
    private static MyGameCanvas myGameCanvas;
    private Thread t;

    /**
     * Specifies what happens when starting the application.
     */
    public void startApp() {
        display = Display.getDisplay(this);
        display.setCurrent(getMainMenu());
        t = new Thread(this);
        t.start();
    }

    /**
     * Specifies what happens when pausing the application.
     */
    public void pauseApp() {
    }

    /**
     * Specifies what happens when exiting the application.
     * @param unconditional 
     */
    public void destroyApp(boolean unconditional) {
        //game.getLevelsRecordStore().closeRecordStore();
        notifyDestroyed();
    }

    /**
     * 
     * @return the display.
     */
    public static Display getDisplay() {
        return display;
    }

    /**
     * 
     * @return the game.
     */
    public static Game getGame() {
        if (game == null) { //|| game.getLevels() == null) {
            game = new Game();
        }
        return game;
    }

    /**
     * 
     * @return the mainMenu.
     */
    public static synchronized MainMenu getMainMenu() {
        if (mainMenu == null) {
            mainMenu = new MainMenu();
        }
        return mainMenu;
    }

    /**
     * 
     * @return the levelSelectionMenu.
     */
    public static LevelSelectionMenu getLevelSelectionMenu() {
        if (levelSelectionMenu == null) {
            levelSelectionMenu = new LevelSelectionMenu(getGame());
        }
        return levelSelectionMenu;

    }

    /**
     * 
     * @return the myGameCanvas.
     */
    public static MyGameCanvas getMyGameCanvas() {
        if (myGameCanvas == null) {
            myGameCanvas = new MyGameCanvas();
        }
        return myGameCanvas;

    }

    /**
     * Starts the thread of the current display.
     */
    public synchronized void run() {
        while (true) {
            if (display.getCurrent().equals(mainMenu)) {
                if (mainMenu.getT() == null || !mainMenu.getT().isAlive()) {
                    mainMenu.init();
                }
                if (getMainMenu().isQuitRequest()) {
                    destroyApp(true);
                }
            }
            else if (display.getCurrent().equals(levelSelectionMenu)) {
                if (levelSelectionMenu.getT() == null || !levelSelectionMenu.getT().isAlive()) {
                    levelSelectionMenu.init();
                }
            }
            else if (display.getCurrent().equals(myGameCanvas)) {
                if (myGameCanvas.getT() == null || !myGameCanvas.getT().isAlive()) {
                    myGameCanvas.init(game.getLevels()[game.getChosenLevel()]);
                }
            }
            try {
                Thread.sleep(10);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    }    
}
现在您可以看到,我首先在这一行初始化变量main菜单

display.setCurrent(getMainMenu());
然后才调用run方法,但有时它会在这一行上给我空指针异常

if (display.getCurrent().equals(mainMenu))

因此,在创建主菜单之前,线程就被调用了,我不知道如何调用,我尝试的是使run方法和getMainMenu方法同步,但没有效果,有人能帮我吗?

在创建主菜单之前,线程没有被调用。调用setCurrent后,在显示可见之前实际上有一个延迟。文档说:“setCurrent()方法立即返回,而不等待更改发生。由于这种延迟,在调用setCurrent()后不久调用getCurrent()不太可能返回传递给setCurrent()的值。”

那么,最好的解决方法是什么?在启动线程之前添加睡眠调用?睡眠调用在startApp方法中是危险的,因为它是来自系统的回调,不应该被长时间阻止。我将在run()方法中处理它,例如,
if(display.getCurrent()==null){/*尚未初始化*/}或者如果display.getCurrent().equals(main菜单)){…