LibGDX Texture rendering problem

I am having trouble rendering a simple texture to the screen using LibGDX, what am I missing here? Everywhere I look the same method is used, but every time I run, I get: Exception in thread “LWJGL Application” com.badlogic.gdx.utils.GdxRuntimeException: Couldn’t load file: data/background.png

It’s one class, so nothing fancy, I don’t know what I’m doing wrong.

package com.pixelPower.space;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class Game implements ApplicationListener {
	public static int WIDTH;
	public static int HEIGHT;
	
	public static OrthographicCamera camera;
	
	private Texture texture;
	private SpriteBatch batch;
	
	public void create() {
		WIDTH = Gdx.graphics.getWidth();
		HEIGHT = Gdx.graphics.getHeight();
		
		batch = new SpriteBatch();
		texture = new Texture(Gdx.files.internal("data/background.png"));
		
		camera = new OrthographicCamera(WIDTH, HEIGHT);
		camera.translate(WIDTH / 2, HEIGHT / 2);
		camera.update();
	}
	
	public void render() {
		Gdx.gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		
		batch.begin();
		batch.draw(texture, 0, 0);
		batch.end();
	}
	
	public void resize(int width, int height) {
		
	}
	
	public void pause() {
		
	}
	
	public void resume() {
		
	}
	
	public void dispose() {
		
	}
}

Nothing wrong with the code, can you show your eclipse project structure with assets? Remember to refresh the eclipse project. Is the data folder in the assets folder of the android project?

Make sure the src for desktop and android project are linked.

Also ensure that you have refreshed and cleaned the project.

To be clear: it probably expects your file to be in the following location:

yourgame-android/assets/data/background.png

and not in e.g. yourgame/data/background.png or yourgame-desktop/data/background.png

Incorrect, LibGDX src folder is set to Assets, if configured properly. You only need to tell it where in there it is.

I’m pretty sure what Gruunt said is correct, what does the src folder have to do with anything.

Because it is where

Gdx.files.internal();

Looks? It points to the src folder within the android project and in the assets folder.

Works fine, this uses the same file handle as gdx.files.internal.

		/* Weapons */
		assets.load("data/weapons/guns/pistol.png", Texture.class);
		assets.load("data/weapons/guns/pistol_small.png", Texture.class);
		assets.load("data/weapons/guns/ak-47.png", Texture.class);
		assets.load("data/weapons/guns/sawnoff.png", Texture.class);
		assets.load("data/items/bullet.png", Texture.class);

Crash:

		/* Weapons */
		assets.load("assets/data/weapons/guns/pistol.png", Texture.class);
		assets.load("assets/data/weapons/guns/pistol_small.png", Texture.class);
		assets.load("assets/data/weapons/guns/ak-47.png", Texture.class);
		assets.load("assets/data/weapons/guns/sawnoff.png", Texture.class);
		assets.load("assets/data/items/bullet.png", Texture.class);
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load dependencies of asset: assets/data/weapons/guns/pistol.png
	at com.badlogic.gdx.assets.AssetManager.handleTaskError(AssetManager.java:526)
	at com.badlogic.gdx.assets.AssetManager.update(AssetManager.java:356)
	at net.gibbo.code15.screens.LoadingScreen.render(LoadingScreen.java:83)
	at com.badlogic.gdx.Game.render(Game.java:46)
	at net.gibbo.code15.Start.render(Start.java:43)
	at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:208)
	at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:115)
Caused by: com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load dependencies of asset: assets/data/weapons/guns/pistol.png
	at com.badlogic.gdx.assets.AssetLoadingTask.handleAsyncLoader(AssetLoadingTask.java:119)
	at com.badlogic.gdx.assets.AssetLoadingTask.update(AssetLoadingTask.java:89)
	at com.badlogic.gdx.assets.AssetManager.updateTask(AssetManager.java:463)
	at com.badlogic.gdx.assets.AssetManager.update(AssetManager.java:354)
	... 5 more
Caused by: com.badlogic.gdx.utils.GdxRuntimeException: com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load file: assets/data/weapons/guns/pistol.png
	at com.badlogic.gdx.utils.async.AsyncResult.get(AsyncResult.java:46)
	at com.badlogic.gdx.assets.AssetLoadingTask.handleAsyncLoader(AssetLoadingTask.java:117)
	... 8 more
Caused by: com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load file: assets/data/weapons/guns/pistol.png
	at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:140)
	at com.badlogic.gdx.assets.loaders.TextureLoader.loadAsync(TextureLoader.java:72)
	at com.badlogic.gdx.assets.loaders.TextureLoader.loadAsync(TextureLoader.java:41)
	at com.badlogic.gdx.assets.AssetLoadingTask.call(AssetLoadingTask.java:69)
	at com.badlogic.gdx.assets.AssetLoadingTask.call(AssetLoadingTask.java:34)
	at com.badlogic.gdx.utils.async.AsyncExecutor$2.call(AsyncExecutor.java:65)
	at java.util.concurrent.FutureTask.run(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)
Caused by: com.badlogic.gdx.utils.GdxRuntimeException: File not found: assets\data\weapons\guns\pistol.png (Internal)
	at com.badlogic.gdx.files.FileHandle.read(FileHandle.java:134)
	at com.badlogic.gdx.files.FileHandle.readBytes(FileHandle.java:218)
	at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:137)
	... 9 more

You’re talking about the root directory. There is no src other than where your actual source code goes.

Yeah sorry, still my post is relevant.

Gdx.files.internal refers to the assets folder, of course putting assets in the path throws an exception. There’s no src folder involved, unless you somehow changes what internal refers to.

One thing that is easy to overlook (Gibbo did mention it above) is that after you add a file to the assets folder you have to refresh, even if the file already is visible in the eclipse file tree. I often forget to do this and have to rebuild again. What I do is right click on the android version of the project and select Refresh.

If anybody knows how to get eclipse to always do this before running that would be great to hear.

Yeah sorry, being a turd lol

Yeah I think the OP got confused or something.