2015-10-04 147 views
0

我嘗試編譯這個問題時出現了問題,這是一個遊戲。libGdx屏幕出現錯誤

在異常線程 「LWJGL應用程序」 在com.waflesgames.spaceinvader.Screens.PlayScreen.render(PlayScreen.java:35)顯示java.lang.NullPointerException 在com.badlogic.gdx.Game.render(遊戲.java:46) at com.waflesgames.spaceinvader.SpaceInvader.render(SpaceInvader.java:24) at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:215) at com.badlogic .gdx.backends.lwjgl.LwjglApplication $ 1.run(LwjglApplication.java:120)

這是PlayScreen.java

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(); 
} 
} 

回答

4

你有錯誤的位置:

public PlayScreen(SpaceInvader Game){ 
     this.game = game; 
     ... 

應該遊戲,而不是遊戲 - 你是GET SpaceInvader實例你在下面的遊戲變量是將this.game分配給自己,在render方法中導致nullpointer異常(遊戲是空的,所以你不能訪問其批號)