[LIBGDX] Texture content lost, black screen

Hi all, I’m developing a game. When the game start I load all the textureAtlas:


package com.me.mygdxgame.game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetDescriptor;
import com.badlogic.gdx.assets.AssetErrorListener;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
import com.badlogic.gdx.utils.Disposable;
import com.me.mygdxgame.util.Constants;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;

public class Assets implements Disposable, AssetErrorListener {

	public static final Assets instance = new Assets();
	private AssetManager assetManager;
	public AssetHead head;
	public AssetFloor floor;
	public AssetLevelDecoration levelDecoration;
	public AssetStecca interaStecca;
	public AssetFonts fonts;
	public AssetForce force;
	public AssetSounds sounds;
	public AssetMusic music;
	public TextureAtlas atlas;

	private Assets() {
	}

	public void init(AssetManager assetManager) {
		this.assetManager = assetManager;
		// set asset manager error handler
		assetManager.setErrorListener(this);
		// load texture atlas
		assetManager.load(Constants.TEXTURE_ATLAS_OBJECTS, TextureAtlas.class);
		// start loading assets and wait until finished
		assetManager.finishLoading();
		atlas = assetManager.get(Constants.TEXTURE_ATLAS_OBJECTS);
		assetManager.finishLoading();

		assetManager.load("sounds/jump.mp3", Sound.class);
		assetManager.finishLoading();
		assetManager.load("sounds/banana.mp3", Sound.class);
		assetManager.finishLoading();
		assetManager.load("sounds/urto.mp3", Sound.class);
		assetManager.finishLoading();
		assetManager.load("music/DST-1990.mp3", Music.class);
		assetManager.finishLoading();
		// enable texture filtering for pixel smoothing
		for (Texture t : atlas.getTextures())
			t.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		// create game resource objects
		head = new AssetHead(atlas);
		floor = new AssetFloor(atlas);
		levelDecoration = new AssetLevelDecoration(atlas);
		interaStecca = new AssetStecca(atlas);
		fonts = new AssetFonts();
		force = new AssetForce(atlas);
		music = new AssetMusic(assetManager);
		sounds = new AssetSounds(assetManager);
		

	}

	

	@Override
	public void dispose() {
		System.out.println("caro assets fai il dispose()");
		assetManager.dispose();
		atlas.dispose();
		fonts.defaultSmall.dispose();
		fonts.defaultNormal.dispose();
		fonts.defaultBig.dispose();

	}

	public class AssetHead {
		public final AtlasRegion head1, head2, head3, head4, head5, head6,
				head10, head12, head13, head14, head15, head11, headUp,
				headDown, head7, head8, head9;

		public AssetHead(TextureAtlas atlas) {
			head1 = atlas.findRegion("scimmia1");
			head2 = atlas.findRegion("scimmia2");
			head3 = atlas.findRegion("scimmia3");
			head4 = atlas.findRegion("scimmia4");
			head5 = atlas.findRegion("scimmia5");
			head6 = atlas.findRegion("scimmia6");
			head7 = atlas.findRegion("scimmia7");
			head8 = atlas.findRegion("scimmia8");
			head9 = atlas.findRegion("scimmia9");
			head10 = atlas.findRegion("scimmia10");
			head11 = atlas.findRegion("scimmia11");
			head12 = atlas.findRegion("scimmia12");
			head13 = atlas.findRegion("scimmia13");
			head14 = atlas.findRegion("scimmia14");
			head15 = atlas.findRegion("scimmia15");
			headUp = atlas.findRegion("scimmiaUp");
			headDown = atlas.findRegion("scimmiaDown");
		}
	}

	public class AssetFloor {
		public final AtlasRegion floor;

		public AssetFloor(TextureAtlas atlas) {
			floor = atlas.findRegion("pavimento");
		}
	}

	public class AssetStecca {
		public final AtlasRegion stecca;
		public final AtlasRegion varco, varco1;

		public AssetStecca(TextureAtlas atlas) {
			stecca = atlas.findRegion("muro");
			varco = atlas.findRegion("banana1");
			varco1 = atlas.findRegion("banana2");
		}
	}

	public class AssetLevelDecoration {
		public final AtlasRegion mountain;

		public AssetLevelDecoration(TextureAtlas atlas) {
			mountain = atlas.findRegion("JungleBackground1");
		}
	}

	public class AssetForce {
		public final AtlasRegion force;
		public final AtlasRegion force_interno;

		public AssetForce(TextureAtlas atlas) {
			force = atlas.findRegion("barra");
			force_interno = atlas.findRegion("rettangolo_interno");
		}
	}

	public class AssetFonts {
		public final BitmapFont defaultSmall;
		public final BitmapFont defaultNormal;
		public final BitmapFont defaultBig;

		public AssetFonts() {
			// create three fonts using Libgdx's 15px bitmap font
			defaultSmall = new BitmapFont(
					Gdx.files.internal("data/font.fnt"), true);
			defaultNormal = new BitmapFont(
					Gdx.files.internal("data/font.fnt"), true);
			defaultBig = new BitmapFont(
					Gdx.files.internal("data/font.fnt"), true);
			// set font sizes
			defaultSmall.setScale(0.75f);
			defaultNormal.setScale(1.0f);
			defaultBig.setScale(2.0f);
			// enable linear texture filtering for smooth fonts
			defaultSmall.getRegion().getTexture()
					.setFilter(TextureFilter.Linear, TextureFilter.Linear);
			defaultNormal.getRegion().getTexture()
					.setFilter(TextureFilter.Linear, TextureFilter.Linear);
			defaultBig.getRegion().getTexture()
					.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		}

	}

	public class AssetSounds {
		public final Sound jump, banana, urto;

		public AssetSounds(AssetManager am) {
			jump = am.get("sounds/jump.mp3", Sound.class);
			banana = am.get("sounds/banana.mp3", Sound.class);
			urto = am.get("sounds/urto.mp3", Sound.class);

		}
	}

	public class AssetMusic {
		public final Music song01;

		public AssetMusic(AssetManager am) {
			song01 = am.get("music/DST-1990.mp3", Music.class);
		}
	}

	@Override
	public void error(AssetDescriptor asset, Throwable throwable) {
		// TODO Auto-generated method stub
		
	}
}


After there are a menu Screen. When I press Play Button:


private void onPlayClicked() {
		ScreenTransition transition = ScreenTransitionFade.init(.75f);
        game.setScreen(new GameScreen(game), transition);
	}

And Game started:


package com.me.mygdxgame.screens;


import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.GL10;
import com.me.mygdxgame.game.Assets;
import com.me.mygdxgame.game.WorldController;
import com.me.mygdxgame.game.WorldRenderer;
import com.me.mygdxgame.util.GamePreferences;
public class GameScreen extends AbstractGameScreen{
	private WorldController worldController;
	private WorldRenderer worldRenderer;
	private boolean paused;
	// pausa
	//private Button btnPause;
	// schermata pausa

	public GameScreen(DirectedGame game) {
		super(game);
	}

	@Override
	public void render(float deltaTime) {
		// Do not update game world when paused.
		if (!paused) {
			// Update game world by the time that has passed
			// since last rendered frame.
			worldController.update(deltaTime);
		}
		// Sets the clear screen color to: Cornflower Blue
		Gdx.gl.glClearColor(0x64 / 255.0f, 0x95 / 255.0f, 0xed / 255.0f,
				0xff / 255.0f);
		// Clears the screen
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		// Render game world to screen
		worldRenderer.render();
	}

	@Override
	public void resize(int width, int height) {
		worldRenderer.resize(width, height);
	}

	@Override
	public void show() {
		GamePreferences.instance.load();
		worldController = new WorldController(game);
		worldRenderer = new WorldRenderer(worldController);
		Gdx.input.setCatchBackKey(true);
		
	}
	
	
	

	
	
	
	@Override
	public void hide() {
		
		System.out.println("walioooooooooooooooooooooooooo sei hideeeeeeeeeeeee");
		worldRenderer.dispose();
		Assets.instance.dispose();
		Gdx.input.setCatchBackKey(false);
		
	}

	@Override
	public void pause() {
		paused = true;
	}

	@Override
	public void resume() {
		super.resume();
		// Only called on Android!
		paused = false;
	}

	@Override
	public InputProcessor getInputProcessor() {
		// TODO Auto-generated method stub
		return worldController;
	}
}


WorldController:


public WorldController(DirectedGame game) {
		this.game=game;
		init();
	}

	private void init() {
		moveCam = 1;
		cameraHelper = new CameraHelper();
		score = 0;
		timeScore = 0;
		levelGenerator = new LevelGenerator();
		cameraHelper.setTarget(null);
		schiaccio = false;

	}

LevelGenerator use the image to build the game world.

When I died in the game:


game.setScreen(new GameOver(game),transition);

And the GameOver class:


package com.me.mygdxgame.screens;


import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Stack;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.me.mygdxgame.game.Assets;
import com.me.mygdxgame.screens.transictions.ScreenTransition;
import com.me.mygdxgame.screens.transictions.ScreenTransitionSlice;
import com.me.mygdxgame.util.Constants;

public class GameOver extends AbstractGameScreen{
	
	
	private Stage stage;
	private Skin skinCanyonBunny;
	private Skin skinLibgdx;
	// menu
	private Image imgBackground;
	private Button btnMenuPlay;
	private Button btnMenuOptions;
	// debug
	private final float DEBUG_REBUILD_INTERVAL = 3.4f;
	private boolean debugEnabled = false;
	private float debugRebuildStage;

	public GameOver(DirectedGame game) {
		super(game);
		
	}

	
	
	@Override
	public void render(float deltaTime) {
		Gdx.gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		if (debugEnabled) {
			debugRebuildStage -= deltaTime;
			if (debugRebuildStage <= 0) {
				debugRebuildStage = DEBUG_REBUILD_INTERVAL;
				rebuildStage();
			}
		}
		stage.act(deltaTime);
		stage.draw();
		Table.drawDebug(stage);
	}

	@Override
	public void resize(int width, int height) {
		stage.setViewport(Constants.VIEWPORT_GUI_WIDTH,
				Constants.VIEWPORT_GUI_HEIGHT, false);
	}

	@Override
	public void show() {

		stage = new Stage();
		rebuildStage();
	}

	@Override
	public void hide() {
		stage.dispose();
		skinCanyonBunny.dispose();
		skinLibgdx.dispose();
	}

	@Override
	public void pause() {
	}

	private void rebuildStage() {
		skinCanyonBunny = new Skin(Gdx.files.internal(Constants.SKIN_UI),
				new TextureAtlas(Constants.TEXTURE_ATLAS_UI));
		skinLibgdx = new Skin(Gdx.files.internal(Constants.SKIN_LIBGDX_UI),
				new TextureAtlas(Constants.TEXTURE_ATLAS_LIBGDX_UI));
		// build all layers
		Table layerBackground = buildBackgroundLayer();
		Table layerControls = buildControlsLayer();
		// assemble stage for menu screen
		stage.clear();
		Stack stack = new Stack();
		stage.addActor(stack);
		stack.setSize(Constants.VIEWPORT_GUI_WIDTH,
				Constants.VIEWPORT_GUI_HEIGHT);
		stack.add(layerBackground);
		stack.add(layerControls);
	}

	private Table buildBackgroundLayer() {
		Table layer = new Table();
		// + Background
		imgBackground = new Image(skinCanyonBunny, "background");
		layer.add(imgBackground);
		return layer;
	}

	private Table buildControlsLayer() {
		Table layer = new Table();
		layer.center();
		// + Play Button
		btnMenuPlay = new Button(skinCanyonBunny, "repeat");
		layer.add(btnMenuPlay);
		btnMenuPlay.addListener(new ChangeListener() {
			@Override
			public void changed(ChangeEvent event, Actor actor) {
				onPlayClicked();
			}
		});
		layer.row();
		// + Options Button
		btnMenuOptions = new Button(skinCanyonBunny, "stop");
		layer.add(btnMenuOptions);
		btnMenuOptions.addListener(new ChangeListener() {
			@Override
			public void changed(ChangeEvent event, Actor actor) {
				onOptionsClicked();

			}
		});
		if (debugEnabled)
			layer.debug();
		return layer;
	}

	
	private void onPlayClicked() {
        game.setScreen(new GameScreen(game));
	}

	private void onOptionsClicked() {
		ScreenTransition transition = ScreenTransitionSlice.init(.75f,ScreenTransitionSlice.UP_DOWN, 10, Interpolation.pow5Out);
        game.setScreen(new MenuScreen(game), transition);
	}



	@Override
	public InputProcessor getInputProcessor() {
		// TODO Auto-generated method stub
		return stage;
	}

}


I died and repeat the game 4-5 times. After these the images aren’t loaded, the screen is all black and the game is very slow. Only the score and musics are loaded. The score use the font (data/font.fnt).

The image references are lost.

help me please!!!

@Override
public void hide() {

  System.out.println("walioooooooooooooooooooooooooo sei hideeeeeeeeeeeee");
  worldRenderer.dispose();
  Assets.instance.dispose();                     <------------------------------------------------------------------ This looks suspicious 
  Gdx.input.setCatchBackKey(false);

}

Lol yep, your disposing everything when the screen is changing :stuck_out_tongue:

nono!
I insert this to try. In both case the result is the same

The problem occurs after 4-5 times that I repeat the game. The Assets class loses the references.

If you post the code in a zip file in a way that is easy to compile and run with a .bat file or something I will take a look at it.

in this moment I can send you the .apk file. with you can see the problem.