AndEngine无法加载菜单场景(OnCreateScene)

时间:2014-01-12 19:55:08

标签: java android andengine

我在andengine中加载菜单场景时遇到问题。我似乎无法弄清楚错误信息/背后的推理。

Log cat:

01-12 14:44:25.883: E/AndEngine(2899): GameActivity.onCreateScene failed. @(Thread: 'GLThread 7465')
01-12 14:44:25.883: E/AndEngine(2899): java.lang.NullPointerException
01-12 14:44:25.883: E/AndEngine(2899):  at com.saleem.dig.scene.SceneManager.setMenuScene(SceneManager.java:31)
01-12 14:44:25.883: E/AndEngine(2899):  at com.saleem.dig.GameActivity.onCreateScene(GameActivity.java:54)
01-12 14:44:25.883: E/AndEngine(2899):  at org.andengine.ui.activity.BaseGameActivity$3.onCreateResourcesFinished(BaseGameActivity.java:171)
01-12 14:44:25.883: E/AndEngine(2899):  at com.saleem.dig.GameActivity.onCreateResources(GameActivity.java:47)
01-12 14:44:25.883: E/AndEngine(2899):  at org.andengine.ui.activity.BaseGameActivity.onCreateGame(BaseGameActivity.java:183)
01-12 14:44:25.883: E/AndEngine(2899):  at org.andengine.ui.activity.BaseGameActivity.onSurfaceCreated(BaseGameActivity.java:112)
01-12 14:44:25.883: E/AndEngine(2899):  at org.andengine.opengl.view.EngineRenderer.onSurfaceCreated(EngineRenderer.java:80)
01-12 14:44:25.883: E/AndEngine(2899):  at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1501)
01-12 14:44:25.883: E/AndEngine(2899):  at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)

我认为可能导致此问题的代码: GameActivity.Java

public class GameActivity extends BaseGameActivity {
    private Camera camera;
    private int CAMERA_WIDTH;
    private int CAMERA_HEIGHT;

    @Override
    public EngineOptions onCreateEngineOptions() {
        // TODO Auto-generated method stub

        CAMERA_WIDTH = this.getResources().getDisplayMetrics().widthPixels;// retrieves
                                                                            // the
                                                                            // native
                                                                            // width
        CAMERA_HEIGHT = this.getResources().getDisplayMetrics().heightPixels;// retrieves
                                                                                // the
                                                                                // native
                                                                                // height
        camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
        EngineOptions engineOptions = new EngineOptions(true,
                ScreenOrientation.LANDSCAPE_FIXED, new FillResolutionPolicy(),
                camera);
        engineOptions.getAudioOptions().setNeedsMusic(true).setNeedsSound(true);
        engineOptions.setWakeLockOptions(WakeLockOptions.SCREEN_ON);
        engineOptions.getTouchOptions().setNeedsMultiTouch(true);

        return engineOptions;

    }

    @Override
    public void onCreateResources(OnCreateResourcesCallback pOnCreateSceneCallback) {
        ResourceManager.prepareManager(getEngine(), this, camera,
                getVertexBufferObjectManager());

        pOnCreateSceneCallback.onCreateResourcesFinished();

    }

    @Override
    public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) {
        // TODO Auto-generated method stub
       SceneManager.getInstance().setMenuScene(pOnCreateSceneCallback);

    }

    @Override
    public void onPopulateScene(Scene arg0, OnPopulateSceneCallback pOnCreateSceneCallback) {
        pOnCreateSceneCallback.onPopulateSceneFinished();
        // TODO Auto-generated method stub

    }

}

SceneManager可以成为查明问题的有用工具:

public class SceneManager {

    private BaseScene menuScene;
    private BaseScene gameScene;

    private static final SceneManager INSTANCE = new SceneManager();

    private BaseScene currentScene;
    private Engine engine = ResourceManager.getInstance().engine;

    public enum SceneType {
        SCENE_MENU,
        SCENE_GAME,
    }

    public void setScene(BaseScene scene) {
        engine.setScene(scene);
        currentScene = scene;
    }

    public void setMenuScene(OnCreateSceneCallback pOnCreateSceneCallback) {
        ResourceManager.getInstance().loadMenuResources();
        menuScene = new MainMenuScene();
        currentScene.createScene();
        pOnCreateSceneCallback.onCreateSceneFinished(menuScene);
    }

    public void setScene(SceneType type) {
        switch (type) {

        case SCENE_MENU:
            setScene(menuScene);
            break;
        case SCENE_GAME:
            setScene(gameScene);
            break;
        }
    }

    public static SceneManager getInstance() {
        return INSTANCE;
    }

    public BaseScene getCurrentScene() {
        return currentScene;
    }
}

1 个答案:

答案 0 :(得分:0)

我认为你没有设置currentScene。因此,当您调用NullPointerException时,您会收到currentScene.createScene(); 您可能需要在currentScene方法中设置onPopulateScene。此外,您需要进行一些修改才能在调用之前初始化它 SceneManager.getInstance().setMenuScene(pOnCreateSceneCallback);