Java libGdx屏幕出现错误
我有一个问题,当我试图编译这是supuse,这将是一个游戏 com.waflesgames.spaceinvainer.Screens.PlayScreen.render(PlayScreen.java:35)上的线程“LWJGL应用程序”java.lang.NullPointerException中出现异常 位于com.badlogic.gdx.Game.render(Game.java:46) 在com.waflesgames.spaceinvader.spaceinvader.render上(spaceinvader.java:24) 位于com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:215) 位于com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120) 这是PlayScreen.javaJava libGdx屏幕出现错误,java,libgdx,screen,Java,Libgdx,Screen,我有一个问题,当我试图编译这是supuse,这将是一个游戏 com.waflesgames.spaceinvainer.Screens.PlayScreen.render(PlayScreen.java:35)上的线程“LWJGL应用程序”java.lang.NullPointerException中出现异常 位于com.badlogic.gdx.Game.render(Game.java:46) 在com.waflesgames.spaceinvader.spaceinvader.render
package com.waflesgames.spaceinvader.Screens;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.waflesgames.spaceinvader.SpaceInvader;
import javax.xml.soap.Text;
/**
* Created by Diego on 03/10/2015.
*/
public class PlayScreen implements Screen{
public SpaceInvader game;
Texture texture;
private OrthographicCamera gamecam;
private Viewport gamePort;
public PlayScreen(SpaceInvader Game){
this.game = game;
texture = new Texture("badlogic.jpg");
gamecam = new OrthographicCamera();
gamePort = new ScreenViewport(gamecam);
}
@Override
public void show() {
}
@Override
public void render(float delta) {
game.batch.setProjectionMatrix(gamecam.combined);
game.batch.begin();
game.batch.draw(texture,0,0);
game.batch.end();
}
@Override
public void resize(int width, int height) {
gamePort.update(width,height);
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void hide() {
}
@Override
public void dispose() {
}
}
这是SpaceInvader.java
package com.waflesgames.spaceinvader;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.waflesgames.spaceinvader.Screens.PlayScreen;
public class SpaceInvader extends Game {
public SpriteBatch batch;
@Override
public void create () {
batch = new SpriteBatch();
setScreen(new PlayScreen(this));
}
@Override
public void render () {
super.render();
}
}
您在此处遇到错误:
public PlayScreen(SpaceInvader Game){
this.game = game;
...
它应该是游戏而不是游戏-您将SpaceInvactor实例获取到游戏变量,因此下面您将this.game分配给它自己,是什么导致渲染方法中出现空指针异常(游戏为空,因此您无法访问其批处理)您在这里有错误:
public PlayScreen(SpaceInvader Game){
this.game = game;
...
它应该是游戏而不是游戏-您将SpaceInvactor实例获取到游戏变量,因此下面您将this.game分配给自身,这是导致渲染方法中出现nullpointer异常的原因(游戏为null,因此您无法访问其批处理)