2014-11-05 71 views
0

我想讓LibGDX拍攝設備屏幕的截圖,然後將截圖加載到雪碧中以供以後渲染。但是,當我嘗試它的應用程序崩潰與NullPointerException。我嘗試了以下鏈接:taking screenshot in libgdxusing ScreenUtils to save screenshot as image in libgdx。當我嘗試爲Sprite創建紋理時,它會與NullPointerException一起崩潰。libgdx截圖到雪碧

我也試過ScreenUtils.getFrameBufferTexture();方法和ScreenUtils.getFrameBufferPixmap()方法,但總是會發生NullPointerException異常。他們碰撞的線條根據我選擇的屏幕截圖的方式而變化,並且我已經確認在拍攝屏幕截圖時確實創建了Pixmap或TextureRegion。當我嘗試將它加載到Texture或Sprite中時,它會因NullPointerException而崩潰。

請不要將此標記爲重複問題,因爲我不是試圖將屏幕截圖保存爲PNG。我試圖直接在Sprite中使用它,它正在崩潰。

代碼和堆棧跟蹤創建TextureRegion:

// create the screen shot 
UT.screenShot = ScreenUtils.getFrameBufferTexture(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); 


// Use screenshot 
screenShot = new Sprite(UT.screenShot); 

我得到這個堆棧跟蹤:在線程

例外 「LWJGL應用程序」 顯示java.lang.NullPointerException

at com.badlogic.gdx.graphics.g2d.TextureRegion.setRegion(TextureRegion.java:112) 
at com.badlogic.gdx.graphics.g2d.Sprite.<init>(Sprite.java:83) 
at *** MY CODE *** CompleteScreen.java:83 
at *** MY CODE *** GameEngine.java:82 
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:206) 
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114) 

這只是我用來創建屏幕截圖的方法之一。但所有的方式都使用ScreenUtils類來獲取屏幕截圖。所有這些方法確實創建了一個截圖。但是,嘗試創建Sprite或Texture時,所有這些方法都會失敗。

代碼和堆棧跟蹤創建像素圖:

// create the screen shot 
UT.screenShot = ScreenUtils.getFrameBufferPixmap(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); 


// Use screenshot 
Texture texture = new Texture(UT.screenShot); 
screenShot = new Sprite(texture); 

我得到這個堆棧跟蹤:在線程

例外 「LWJGL應用程序」 顯示java.lang.NullPointerException

at com.badlogic.gdx.graphics.glutils.PixmapTextureData.<init>(PixmapTextureData.java:37) 
at com.badlogic.gdx.graphics.glutils.PixmapTextureData.<init>(PixmapTextureData.java:32) 
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:104) 
at *** MY CODE *** CompleteScreen.java:84 
at *** MY CODE *** GameEngine.java:82 
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:206) 
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114) 
+4

爲什麼不發佈任何方法的堆棧跟蹤以及代碼?如果沒有任何有關例外的信息以及您的代碼,我們無法提供幫助。 – Springrbua 2014-11-05 09:40:22

回答

0

所以我解決了這個問題只是爲了創造另一個問題。哦編程的樂趣! ;-)無論如何,我會在另一個問題上詢問我的第二個問題。這個問題的答案是我很快就擺脫了Pixmap或TextureRegion。

在12小時編程結束時你會得到什麼樣的代碼?骨頭代碼!

哦,很好的經驗教訓,只有11個小時的程序。 ;-)