Что вызывает исключение NullPointerException в Android? - PullRequest
2 голосов
/ 29 ноября 2011

Я новичок в разработке игр для Android. Я пробовал пример разработки игры, но кое-кто не смог понять причину исключения нулевой точки в Android.

    public void onLoadResources() {
         this.mBitmapTextureAtlas = new BitmapTextureAtlas(512, 512,
               TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");

        this.mPlayerTextureRegion = BitmapTextureAtlasTextureRegionFactory
                .createFromAsset(this.mBitmapTextureAtlas, this, "Player.png",
                0, 0);
        this.mEngine.getTextureManager().loadTexture(this.mBitmapTextureAtlas);
        final int PlayerX = (int)(mCamera.getWidth() / 2);
        final int PlayerY = (int) ((mCamera.getHeight() - mPlayerTextureRegion
                .getHeight()) / 2);
        this.player = new Sprite(PlayerX, PlayerY, this.mPlayerTextureRegion);
        this.player.setScale(2);
        this.mMainScene.attachChild(this.player);
    }

Последняя строка "this.mMainScene.attachChild(this.player);" вызывает исключение нулевой точки, даже если все инициализировано. После комментирования этой строки все работает нормально, но дисплей без спрайта.

вот код, где инициализируется mMainScene

    public Scene onLoadScene() {
        this.mEngine.registerUpdateHandler(new FPSLogger());
        this.mMainScene = new Scene();
        this.mMainScene.setBackground(new ColorBackground(0.09804f, 0.6274f, 0.8784f));
        return mMainScene;
    }

1 Ответ

4 голосов
/ 29 ноября 2011

Вы не инициализировали mMainScene.Вы не можете ссылаться (т. Е. Присоединять методы Children / call, пока не определите, что такое mMainScene.)

Не могли бы вы проверить, имеет ли значение mMainScene значение null, или перенести свое назначение mMainScene в onLoadResources ()?

Можете ли вы переместить определение mMainScene из onLoadScene в onLoadResources?

public void onLoadResources() {
     this.mBitmapTextureAtlas = new BitmapTextureAtlas(512, 512,
           TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");

    this.mPlayerTextureRegion = BitmapTextureAtlasTextureRegionFactory
            .createFromAsset(this.mBitmapTextureAtlas, this, "Player.png",
            0, 0);
    this.mEngine.getTextureManager().loadTexture(this.mBitmapTextureAtlas);
    final int PlayerX = (int)(mCamera.getWidth() / 2);
    final int PlayerY = (int) ((mCamera.getHeight() - mPlayerTextureRegion
            .getHeight()) / 2);
    this.player = new Sprite(PlayerX, PlayerY, this.mPlayerTextureRegion);
    this.player.setScale(2);
    this.mMainScene = new Scene();
    this.mMainScene.setBackground(new ColorBackground(0.09804f, 0.6274f, 0.8784f));
    this.mMainScene.attachChild(this.player);
}

public Scene onLoadScene() {
    return mMainScene;
}

Редактировать:

Основываясь на примере Snake , кажется, что вы должны добавить детей всцена в методе onLoadScene:

    public Scene onLoadScene() {
        this.mEngine.registerUpdateHandler(new FPSLogger());

        this.mScene = new Scene();
        for(int i = 0; i < LAYER_COUNT; i++) {
            this.mScene.attachChild(new Entity());
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...