【问题标题】:What causes NullPointerException in android?什么导致Android中的NullPointerException?
【发布时间】:2011-11-29 03:06:12
【问题描述】:

我是安卓游戏开发的新手。我一直在尝试游戏开发的示例,但有些无法理解 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;
    }

【问题讨论】:

  • 顺便说一下,原因与Java没有什么不同。另请参阅 API 文档:javaandroid
  • 你能把初始化mMainScene的代码贴出来吗?
  • 我按照你的要求编辑了问题

标签: java android nullpointerexception


【解决方案1】:

你还没有初始化 mMainScene。在定义 mMainScene 是什么之前,您无法引用(即附加子项/调用方法。)

您能否检查 mMainScene 是否为空,或者将您对 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());
    }

【讨论】:

  • mMainScene 由 onLoadScene() 在同一个类中初始化,该类扩展了andengine的BaseGameActivity。
  • 应该有关于这些方法的执行顺序的文档吗?
  • 不可以,因为,onLoadScene 可能是从外部调用的,虽然我改变了它
  • 不可以,因为 onLoadScene 可能会从外部调用,虽然我通过 mMainScene=onLoadScene();但这删除了空点异常,但我的精灵不存在!
  • 看看Snake 的这个例子——它在 onLoadScene(第 160、165 行)中添加精灵/场景子项,而不是像你展示的那样在 onLoadResource 中。我也会在上面更新我的回复。
猜你喜欢
  • 1970-01-01
  • 2015-03-04
  • 2012-01-22
  • 1970-01-01
  • 1970-01-01
  • 2010-11-11
  • 1970-01-01
  • 2019-11-01
  • 1970-01-01
相关资源
最近更新 更多