I clearly stored my "background.png" in the assets folder under android.
I also checked that it was there in Finder.
I am working with Android Studio.
However, upon trying to access the image:
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);
}
I get this error:
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......
What am I missing?
EDIT
Also, my current path for the Desktop launcher for the Working Directory is : AndroidStudioProjects/libGDXGame
However, upon changing it to: AndroidStudioProjects/libGDXGame/android/assets
I get a new error like such:
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)
try using this:
textureRegion = new TextureRegion(new Texture(
Gdx.files.internal("background.png")));
if it does not work review, case sensitive, of the file name.
Edit: look that is a copy of the file, not a link, once he happened to someone so wrong
You shouldn't load assets in a constructor directly because Gdx.files is null. Either load the assets outside of the constructor or use the AssetsManager.
Related
I'm building a game in LibGDX and have began adding visual aspects of the game such as the logo. Considering that I've been using the default libgdxXXX.png files as placeholders for my logo, and that I'm replacing them with my logo, I didn't feel it had any more purpose in my program. I decide to delete it; but it decide to not compile and come up with this error:
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load file: assets/libgdx128.png
at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:148)
at com.badlogic.gdx.backends.lwjgl.LwjglGraphics.setupDisplay(LwjglGraphics.java:196)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:142)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:124)
Caused by: com.badlogic.gdx.utils.GdxRuntimeException: File not found: assets/libgdx128.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:145)
... 3 more
Somehow, when I return the files from the Trash (I use Linux), it works again! I have checked my code and found nothing I wrote referred to those files. When I deleted the files 1 by 1, the error changes from libgdx128.png to libgdx64.png to libgdx32.png to libgdx16.png respectively. I have changed all the references from libgdx128.png to my logo.png in my code; yet when I try to delete the libgdx logo and run my program, it fails to compile! Any ideas?
I found out my problem. I forgot all about this code at DesktopLauncher.java:
for (int size : new int[] { 128, 64, 32, 16 }) {
configuration.addIcon("assets/libgdx" + size + ".png", FileType.Internal);
}
I did not know it was referencing to the files I have deleted. The configuration.addIcon(); was the code setting the program icon - similar to how a favicon works. I will be adding my own icon now that I know what the code does.
Hope this helps anyone else confused about the error.
I'm trying to make basic game in Eclipse libGDX. I have Problem with rendering sprite. I do everything like it shoud be, but I still get this Error:
LwjglGraphics: created OpenGL 3.2+ core profile (GLES 3.0) context.
This is experimental! Exception in thread "LWJGL Application"
com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load file: BG.png
at com.badlogic.gdx.graphics.Pixmap.(Pixmap.java:148) at
com.badlogic.gdx.graphics.TextureData$Factory.loadFromFile(TextureData.java:98)
at com.badlogic.gdx.graphics.Texture.(Texture.java:100) at
com.badlogic.gdx.graphics.Texture.(Texture.java:92) at
com.zebrabandit.egghead.Assets.Load(Assets.java:13) at
com.zebrabandit.egghead.GameScreen.(GameScreen.java:24) at
com.zebrabandit.egghead.EggHead.create(EggHead.java:10) at
com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:147)
at
com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:124)
Caused by: com.badlogic.gdx.utils.GdxRuntimeException: File not found:
BG.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.(Pixmap.java:145) ... 8 more
And here is my CODE:
public static Texture backgroundT;
public static Sprite backgroundS;
...
backgroundT = new Texture(Gdx.files.internal("BG.png"));
backgroundT.setFilter(TextureFilter.Linear, TextureFilter.Linear);
backgroundS = new Sprite(backgroundT);
backgroundS.flip(false, true);
...
#Override
public void render(float delta) {
Gdx.gl.glClearColor(1F, 1F, 1F, 1F);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
//rendering code :D
batch.draw(Assets.backgroundS, 0, 0);
batch.end();
}
I assume you have initialized your project with the setup application Tool
(gdx-setup.jar).
Then it's possible you are experiencing the working directory setup problem.
That's what the error says(Couldn't load file)
In IDEA IDE I have to edit configuration so the working directory points to folder in project: android\assets
File not found question was answered here
For further reference please check the video Mario Zechner has recorder to ease work the Eclipse users:
Running and Debugging in Eclipse
Couldn't load file: BG.png at com.badlogic.gdx.graphics.Pixmap.(Pixmap.java:148) at ...
I think your problem is you are trying to load a file, that is not there. Are you absolutely sure that there is a file in that location Gdx.files.internal("BG.png").
Also you will have many problems if you are trying to load assets with uppercase characters in their names on android. So if your file is there, try renaming it to all lowercase characters.
So I have recently started learning libgdx in Android Studio. I am using some extensions like box2d. To create some nice borders around the object, I am using Physics Body Editor. So, as there were many errors in my own code, i have just copied one from here: Code Sample. But still, I keep getting an error on the following line:
BodyEditorLoader loader = new BodyEditorLoader(Gdx.files.internal("tankA.json"));
The file tankA.json is in my assets folder.
Here is my error:
02-12 08:27:52.216 13720-13734/com.tynibattles04.game.android E/AndroidRuntime﹕ FATAL EXCEPTION: GLThread 6581
Process: com.tynibattles04.game.android, PID: 13720
com.badlogic.gdx.utils.GdxRuntimeException: Error reading file: tankA.json (Internal)
at com.badlogic.gdx.backends.android.AndroidFileHandle.read(AndroidFileHandle.java:77)
at com.badlogic.gdx.files.FileHandle.length(FileHandle.java:605)
at com.badlogic.gdx.backends.android.AndroidFileHandle.length(AndroidFileHandle.java:220)
at com.badlogic.gdx.files.FileHandle.estimateLength(FileHandle.java:233)
at com.badlogic.gdx.files.FileHandle.readString(FileHandle.java:198)
at com.badlogic.gdx.files.FileHandle.readString(FileHandle.java:191)
at aurelienribon.bodyeditor.BodyEditorLoader.<init>(BodyEditorLoader.java:41)
at com.tynibattles04.game.TinyBattles.createBottle(TinyBattles.java:127)
at com.tynibattles04.game.TinyBattles.create(TinyBattles.java:74)
at com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:241)
at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1512)
at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)
Caused by: java.io.FileNotFoundException: tnkA.json
at android.content.res.AssetManager.openAsset(Native Method)
at android.content.res.AssetManager.open(AssetManager.java:355)
at android.content.res.AssetManager.open(AssetManager.java:329)
at com.badlogic.gdx.backends.android.AndroidFileHandle.read(AndroidFileHandle.java:75)
Do you know what could i do with all that?
Thanks in advance!
I have accidentally misspelled tankA.json in the error code - thanks to Angel Angel. But I still keep getting the same error. Maybe that's be cause something wrong this the BodyEditorLoader?
Check the file name and the name you use to load, then you say:
BodyEditorLoader loader = new BodyEditorLoader(
Gdx.files.internal("tankA.json"));
But in stack says:
com.badlogic.gdx.utils.GdxRuntimeException:
Error reading file:
tnkA.json (Internal)
maybe this is an error in the file name: tnkA.json
note: maybe in your code use, tnkA.json and not tankA.json as you think.
hope it helps.
I have a problem while creating selectbox on libgdx.
Skin.add("button", new Texture("data/menu/button.png"));
[...]
SelectBoxStyle selectboxstyle = new SelectBoxStyle();
selectboxstyle.font = new BitmapFont(Gdx.files.internal("data/fonts/menufont.fnt"), Gdx.files.internal("data/fonts/menufont.png"), false);`
selectboxstyle.background = Skin.getDrawable("button");
SelectBox dropdown = new SelectBox(new String[] {"item1", "item2"}, selectboxstyle);
The last line throw exception:
Exception in thread "LWJGL Application" java.lang.NullPointerException
at
com.badlogic.gdx.scenes.scene2d.ui.SelectBox.setItems(SelectBox.java:140)
at
com.badlogic.gdx.scenes.scene2d.ui.SelectBox.(SelectBox.java:71)
at com.me.Terminum.Screens.MainMenu.ShowVideo(MainMenu.java:171) at
com.me.Terminum.Screens.MainMenu.show(MainMenu.java:121) at
com.badlogic.gdx.Game.setScreen(Game.java:62) at
com.me.Terminum.Screens.SplashScreen.show(SplashScreen.java:88) at
com.badlogic.gdx.Game.setScreen(Game.java:62) at
com.me.Terminum.App.create(App.java:13) at
com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:136)
at
com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
I checked it on libgdx 0.9.8 and 0.9.9. What am I do wrong?
I found the answer.
Everything that is not marked as optional must be populated. And additionaly, ScrollPaneStyle.background must be filled (even is it marked as optional). Without this, libgdx threw me an exception.
I found this java source code for a brick breaker game online. I downloaded it and tried to run it but it gives me a NullPointerException. What is giving this exception and how do I fix it? Thanks.
http://zetcode.com/tutorials/javagamestutorial/breakout/
Here is the stacktrace:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:181)
at Ball.<init>(Ball.java:16)
at Board.gameInit(Board.java:48)
at Board.addNotify(Board.java:43)
at java.awt.Container.addNotify(Container.java:2578)
at javax.swing.JComponent.addNotify(JComponent.java:4685)
at java.awt.Container.addNotify(Container.java:2578)
at javax.swing.JComponent.addNotify(JComponent.java:4685)
at java.awt.Container.addNotify(Container.java:2578)
at javax.swing.JComponent.addNotify(JComponent.java:4685)
at javax.swing.JRootPane.addNotify(JRootPane.java:739)
at java.awt.Container.addNotify(Container.java:2578)
at java.awt.Window.addNotify(Window.java:663)
at java.awt.Frame.addNotify(Frame.java:470)
at java.awt.Window.show(Window.java:859)
at java.awt.Component.show(Component.java:1584)
at java.awt.Component.setVisible(Component.java:1536)
at java.awt.Window.setVisible(Window.java:842)
at Breakout.<init>(Breakout.java:14)
at Breakout.main(Breakout.java:18)
Exception in thread "Timer-0" java.lang.NullPointerException
at Board$ScheduleTask.run(Board.java:110)
at java.util.TimerThread.mainLoop(Timer.java:512)
at java.util.TimerThread.run(Timer.java:462)
Apparently this code is supposed to work, but it isn't.
You probably didn't download the images as well, or at least didn't put them in the right folder. Make sure you put the images here:
Browse to Ball.java, go two folder levels up, create there a folder called images and put there your images.