【问题标题】:Attempt to read from field 'com.badlogic.gdx.graphics.Texture com.badlogic.gdx.graphics.g2d.TextureRegion.texture' on a null object reference尝试从空对象引用上的字段“com.badlogic.gdx.graphics.Texture com.badlogic.gdx.graphics.g2d.TextureRegion.texture”读取
【发布时间】:2021-08-02 19:51:23
【问题描述】:

我的游戏已在游戏市场上发布。每天都会出现同样的错误。我不明白它是从哪里来的。也许有人有类似的东西?

Fatal Exception: java.lang.NullPointerException: Attempt to read from field 'com.badlogic.gdx.graphics.Texture com.badlogic.gdx.graphics.g2d.TextureRegion.texture' on a null object reference
   at com.badlogic.gdx.graphics.g2d.SpriteBatch.draw(SpriteBatch.java:640)
   at durak.cards.game.actors.ButtonsActor.draw(ButtonsActor.java:239)
   at com.badlogic.gdx.scenes.scene2d.Group.drawChildren(Group.java:111)
   at com.badlogic.gdx.scenes.scene2d.Group.draw(Group.java:58)
   at com.badlogic.gdx.scenes.scene2d.Group.drawChildren(Group.java:111)
   at com.badlogic.gdx.scenes.scene2d.Group.draw(Group.java:58)
   at com.badlogic.gdx.scenes.scene2d.Stage.draw(Stage.java:129)
   at durak.cards.game.screens.GameScreen.render(GameScreen.java:669)
   at com.badlogic.gdx.Game.render(Game.java:46)
   at durak.cards.game.StartGame.render(StartGame.java:151)
   at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(AndroidGraphics.java:471)
   at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1573)
   at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1272)

我用的是AssetManager,然后在ButtonsActor类中解压图集

public class ButtonsActor extends Actor {

Sprite sprite;
TextureAtlas textureAtlas;
TextureRegion[] textureRegions ;


public ButtonsActor() {
    textureAtlas = new   TextureAtlas ();
    textureAtlas = AssetsManager.button;
    textureRegions = new TextureRegion[41];
    textureRegions[0] = (textureAtlas.findRegion("buttonMinus"));
    textureRegions[1] = (textureAtlas.findRegion("sum"));
    textureRegions[2] = (textureAtlas.findRegion("buttonPlus"));
    textureRegions[3] = (textureAtlas.findRegion("2_users"));
    textureRegions[4] = (textureAtlas.findRegion("3_users"));
    textureRegions[5] = (textureAtlas.findRegion("4_users"));
    textureRegions[6] = (textureAtlas.findRegion("frameMenu"));
    textureRegions[7] = (textureAtlas.findRegion("buttonPlay"));
    textureRegions[8] = (textureAtlas.findRegion("backButton"));
  ///////////////////////////////////////////////////////////////////
    textureRegions[40] = (textureAtlas.findRegion("buttonInfo"));

}

public void spriteChangeB(int tempInt) {
    sprite = new Sprite(textureRegions[tempInt]);
}
@Override
public Actor hit (float x, float y, boolean touchable) {
    if (getTouchable() != Touchable.enabled) return null;
    return x >= 0 && x < this.getWidth() && y >= 0 && y < this.getHeight() ? this : null;
}


@Override
public void draw(Batch batch, float parentAlpha) {

    Color color = getColor();
    batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
    batch.draw(sprite, getX(), getY(), getOriginX(), getOriginY(),
            getWidth(), getHeight(), getScaleX(), getScaleY(), getRotation());
}

}

我从 ButtonsActor 类中删除了所有包含演员的组。现在我只使用舞台,不使用组。但错误仍然出现。

【问题讨论】:

  • 你是如何将纹理加载到游戏中的?当您退出游戏而不杀死它时,您的游戏中发生了什么?您能否提供 ButtonsActors 类或至少方法 draw()
  • 我添加了信息

标签: libgdx


【解决方案1】:

您是否只使用AssetManager.load(...),因为这只是-开始-加载队列。

AssetManager.finishLoading() 同步完成,或者您可以使用AssetManager.isFinished() 进行检查。

由于某些手机具有不同的读取性能,因此很可能有一些没有及时完成。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-14
    • 1970-01-01
    • 1970-01-01
    • 2019-11-04
    • 2020-08-06
    • 1970-01-01
    • 1970-01-01
    • 2022-07-03
    相关资源
    最近更新 更多