Button com.badlogic.gdx.graphics.g2d.TextureRegion.setRegion LIBGDX上的NullPointerException

Button com.badlogic.gdx.graphics.g2d.TextureRegion.setRegion LIBGDX上的NullPointerException,button,input,nullpointerexception,libgdx,Button,Input,Nullpointerexception,Libgdx,当玩家与特定硬币发生碰撞(如马里奥游戏)时,我尝试生成一个项目(蘑菇)时,会出现空指针异常。我一直在遵循教程,但我希望输入是按钮,而不是键。我已经创建了4个按钮并添加到舞台上。输入工作正常。但我有个例外 我只是简单地将按钮添加到舞台上,输入仍然使用按键 我也不例外 我删除了代码中的所有按钮,输入使用了键 这是我在舞台上添加按钮的代码 public class PlayScreen implements Screen, InputProcessor { .

当玩家与特定硬币发生碰撞(如马里奥游戏)时,我尝试生成一个项目(蘑菇)时,会出现空指针异常。我一直在遵循教程,但我希望输入是按钮,而不是键。我已经创建了4个按钮并添加到舞台上。输入工作正常。但我有个例外

我只是简单地将按钮添加到舞台上,输入仍然使用按键

我也不例外

我删除了代码中的所有按钮,输入使用了键

这是我在舞台上添加按钮的代码

        public class PlayScreen implements Screen, InputProcessor

      {
      .......
     private Array<Item> items;                                             
     private LinkedBlockingQueue<ItemDef> itemsToSpawn; 
     private TextureAtlas atlas = new TextureAtlas("move_sprites.pack"); //contains the mushroom as well as the player packed together  


    public PlayScreen(MyGame game) {
           .............
    stage = new Stage();
    atlas = new TextureAtlas("ui/button_pack.pack");                                                                                                      
    skin = new Skin(atlas);
    white = new BitmapFont(Gdx.files.internal("fonts/white.fnt"), false);
    black = new BitmapFont(Gdx.files.internal("fonts/black.fnt"), false);

    TextButton.TextButtonStyle textButtonStyle = new TextButton.TextButtonStyle();
    textButtonStyle.up = skin.getDrawable("buttonin");
    textButtonStyle.down = skin.getDrawable("buttonout");
    textButtonStyle.pressedOffsetX = 1;
    textButtonStyle.font = black;
    buttonright = new TextButton("Right", textButtonStyle);
    buttonleft = new TextButton("Left", textButtonStyle);
    //table.setBounds(500, 5, 250, 150);
    buttonleft.setPosition(350, 5);
    buttonleft.setHeight(100);
    buttonleft.setWidth(100);
    buttonright.setPosition(100, 5);
    buttonright.setHeight(100);
    buttonright.setWidth(100);

    buttonup = new TextButton("up", textButtonStyle);
    buttonup.setPosition(600, 5);
    buttonup.setHeight(100);
    buttonup.setWidth(100);
    stage.addActor(buttonup);
    stage.addActor(buttonright);
    stage.addActor(buttonleft);
    Gdx.input.setInputProcessor(stage);

        ......
}
//使用按钮提供输入

   public void handleInput(float dt)
    {
    if (gamehero.currentState != Hero.State.DEAD) {

       buttonup.addListener(new InputListener() {
        @Override
    public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {

       gamehero.heroBody.setLinearVelocity(0, 4f);

                }
        return true;
          }
     }
        );
     }
}
public void render(float delta) 
     {
  ......
    update(delta); 
    stage.act();
    stage.draw();
       }
   }
}
蘑菇类

   public class Mushroom extends Item {
   public Mushroom(PlayScreen screen, float x, float y) {
    super(screen, x, y);
//糊状物是使用纹理包装机包装在一起的雪碧之一。异常在下面的行中抛出

    setRegion(screen.getAtlas().findRegion("mush"), 0, 0, 16, 16);      
    velocity = new Vector2(0.7f, 0); //velocity of mushroom             
}
当马里奥的头与下面的硬币相撞时,我触发了这个蘑菇

  public class Coins extends InteractiveTileObject {
  ....

   public void onHeadHit(Hero hero) {
    if (object.getProperties().containsKey("mushroom"))       {
        Gdx.app.log("coins", "collision");
//下一行也出现异常。发生冲突时,将在主类中调用spawnItem方法

        screen.spawnItem(new ItemDef(new Vector2(body.getPosition().x, body.getPosition().y + 16 / MyGame.PPM), Mushroom.class));     
    }
ItemDef类

public class ItemDef {
public Vector2 position;           
public Class<?> type;                

public ItemDef(Vector2 position, Class<?> type)            
     {
    this.position = position;                      
    this.type = type;                                
    }
公共类ItemDef{
公共向量2位置;
公共类类型;
公共项定义(矢量2位置,类类型)
{
这个位置=位置;
this.type=type;
}
}


如果我只是在代码中添加一个按钮并使用键输入,就会发生异常。如果我只是在代码中添加一个按钮,那么问题在于生成蘑菇。很奇怪。请帮忙。我是初学者。提前感谢

我找到了此异常的原因。 我在声明时为2个TextureAtlas提供了相同的名称

 private TextureAtlas atlas = new TextureAtlas("move_sprites.pack");
另一个呢,

atlas = new TextureAtlas("ui/button_pack.pack");  

但我指的是另一节课上的第一节课。因为我只声明了一次TextureAtlas,但用于两个不同的包,所以它抛出了null指针异常,因为存在名称冲突。谢谢你的回答。

你的
公共无效更新
方法是在游戏的渲染循环中执行的吗?如果是这样,您不应该在这里添加新的输入侦听器,而是在按钮初始化之后添加。
mush
可能在您的atlas中不存在。或者从
screen.getAtlas()
返回的atlas此时仍然为空。在该行上放置一个断点,并调查实际为null的内容。然后通过确保正确初始化来修复它。你的控件与你的atlas无关,请问清楚具体的问题。如果screen.getAtlas()为空,那么如果我移除按钮并使用keys@Menno-Gouw提供输入,它将如何毫无例外地执行
atlas = new TextureAtlas("ui/button_pack.pack");