LibGDX无法访问内部图像资源

时间:2015-01-25 21:09:39

标签: java android oop android-studio libgdx

我清楚地存储了我的" background.png"在android下的assets文件夹中。 我还检查过它在Finder中。

我正在使用Android Studio。

然而,在尝试访问图像时:

public Background() {
    textureRegion = new TextureRegion(new Texture("background.png"));
    textureRegionBounds1 = new Rectangle(0 - Constants.APP_WIDTH / 2, 0, Constants.APP_WIDTH, Constants.APP_HEIGHT);
    textureRegionBounds2 = new Rectangle(Constants.APP_WIDTH / 2, 0, Constants.APP_WIDTH, Constants.APP_HEIGHT);
}

我收到此错误:

Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load file: background.png
at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:140)
at com.badlogic.gdx.graphics.TextureData$Factory.loadFromFile(TextureData.java:98)
at com.badlogic.gdx.graphics.GLTexture.createTextureData(GLTexture.java:185)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:103)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:95)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:91)
at com.com.waylonhuang.game.actors.Background.<init>(Background.java:21)
at com.com.waylonhuang.game.stages.GameStage.setUpBackground(GameStage.java:66)
at com.com.waylonhuang.game.stages.GameStage.setUpWorld(GameStage.java:59)
at com.com.waylonhuang.game.stages.GameStage.<init>(GameStage.java:51)
at com.mygdx.game.GameScreen.<init>(GameScreen.java:16)
at com.mygdx.game.DrifterGame.create(DrifterGame.java:11)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:137)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
Caused by: com.badlogic.gdx.utils.GdxRuntimeException: File not found: background.png (Internal)
    at com.badlogic.gdx.files.FileHandle.read(FileHandle.java:136)
    at com.badlogic.gdx.files.FileHandle.readBytes(FileHandle.java:222)
    at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:137)
    ... 13 more......

我错过了什么?

修改 此外,我目前工作目录桌面启动器的路径是:AndroidStudioProjects / libGDXGame

然而,在将其更改为:AndroidStudioProjects / libGDXGame / android / assets

我收到了类似的新错误:

Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.com.waylonhuang.game.stages.GameStage.draw(GameStage.java:134)
at com.mygdx.game.GameScreen.render(GameScreen.java:25)
at com.badlogic.gdx.Game.render(Game.java:46)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:208)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)

2 个答案:

答案 0 :(得分:1)

尝试使用:

textureRegion = new TextureRegion(new Texture(
                Gdx.files.internal("background.png")));

如果它不起作用,则检查文件名的区分大小写。

编辑:看一下该文件的副本,而不是链接,一旦他遇到某个错误的人

答案 1 :(得分:0)

您不应直接在构造函数中加载资源,因为Gdx.files为null。将资产加载到构造函数之外或使用AssetsManager