HoopsMcCann
HoopsMcCann

Reputation: 347

LibGDX Game Works as Expected on Desktop, But Not on Android Device

I'm running into a bit of a problem. I'm a bit new to libGDX and have little idea of whats wrong.
Here I have a state that has three instances of a text button. It works fine on my desktop, but crashes on my phone. I'm not sure what the problem is, but when I comment out the code for two of the instances, it works on my phone.

public class MenuStateTest extends State {

Stage stage;
TextButton button, button2, button3;
TextButton.TextButtonStyle textButtonStyle;
BitmapFont font;
Skin skin;
TextureAtlas buttonAtlas, button2Atlas, button3Atlas;

public MenuStateTest(GameStateManager gsm) {
    super(gsm);
    stage = new Stage();
    Gdx.input.setInputProcessor(stage);
    font = new BitmapFont();
    skin = new Skin();

    buttonAtlas = new TextureAtlas("buttonTest.pack");
    skin.addRegions(buttonAtlas);
    textButtonStyle = new TextButton.TextButtonStyle();
    textButtonStyle.font = font;
    textButtonStyle.up = skin.getDrawable("1BBlock");
    textButtonStyle.down = skin.getDrawable("T1BBlock");
    button = new TextButton("", textButtonStyle);
    stage.addActor(button);
    button.setPosition(20,200);

    button2Atlas = new TextureAtlas("button2Test.pack");
    skin.addRegions(button2Atlas);
    textButtonStyle = new TextButton.TextButtonStyle();
    textButtonStyle.font = font;
    textButtonStyle.up = skin.getDrawable("2BBlock");
    textButtonStyle.down = skin.getDrawable("T2BBlock");
    button2 = new TextButton("", textButtonStyle);
    stage.addActor(button2);
    button2.setPosition(175,200);

    button3Atlas = new TextureAtlas("button3Test.pack");
    skin.addRegions(button3Atlas);
    textButtonStyle = new TextButton.TextButtonStyle();
    textButtonStyle.font = font;
    textButtonStyle.up = skin.getDrawable("3BBlock");
    textButtonStyle.down = skin.getDrawable("T3BBlock");
    button3 = new TextButton("", textButtonStyle);
    stage.addActor(button3);
    button3.setPosition(330,200);
}

@Override
protected void render(SpriteBatch sb) {
    stage.draw();

Any help would be appreciated.
Thanks.

EDIT: Here's what I was able to get from the logcat

03-23 11:44:54.510 25775-25799/? E/AndroidRuntime: FATAL EXCEPTION: GLThread 2379
                                               Process: com.mygame.game.android, PID: 25775
                                               Theme: themes:{default=overlay:com.blazze11.euphoria.dark, iconPack:system, fontPkg:system, com.android.systemui=overlay:com.blazze11.euphoria.dark, com.android.systemui.navbar=overlay:system}
                                               com.badlogic.gdx.utils.GdxRuntimeException: Error reading file: button2Test.pack (Internal)
                                                   at com.badlogic.gdx.backends.android.AndroidFileHandle.read(AndroidFileHandle.java:77)
                                                   at com.badlogic.gdx.graphics.g2d.TextureAtlas$TextureAtlasData.<init>(TextureAtlas.java:103)
                                                   at com.badlogic.gdx.graphics.g2d.TextureAtlas.<init>(TextureAtlas.java:231)
                                                   at com.badlogic.gdx.graphics.g2d.TextureAtlas.<init>(TextureAtlas.java:226)
                                                   at com.badlogic.gdx.graphics.g2d.TextureAtlas.<init>(TextureAtlas.java:216)
                                                   at com.badlogic.gdx.graphics.g2d.TextureAtlas.<init>(TextureAtlas.java:211)
                                                   at com.mygame.game.States.MenuStateTest.<init>(MenuStateTest.java:41)
                                                   at com.mygame.game.MyGame.create(MyGame.java:29)
                                                   at com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:243)
                                                   at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1519)
                                                   at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)
                                                Caused by: java.io.FileNotFoundException: button2Test.pack
                                                   at android.content.res.AssetManager.openAsset(Native Method)
                                                   at android.content.res.AssetManager.open(AssetManager.java:331)
                                                   at android.content.res.AssetManager.open(AssetManager.java:305)
                                                   at com.badlogic.gdx.backends.android.AndroidFileHandle.read(AndroidFileHandle.java:75)
                                                   at com.badlogic.gdx.graphics.g2d.TextureAtlas$TextureAtlasData.<init>(TextureAtlas.java:103) 
                                                   at com.badlogic.gdx.graphics.g2d.TextureAtlas.<init>(TextureAtlas.java:231) 
                                                   at com.badlogic.gdx.graphics.g2d.TextureAtlas.<init>(TextureAtlas.java:226) 
                                                   at com.badlogic.gdx.graphics.g2d.TextureAtlas.<init>(TextureAtlas.java:216) 
                                                   at com.badlogic.gdx.graphics.g2d.TextureAtlas.<init>(TextureAtlas.java:211) 
                                                   at com.mygame.game.States.MenuStateTest.<init>(MenuStateTest.java:41) 
                                                   at com.mygame.game.MyGame.create(MyGame.java:29) 
                                                   at com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:243) 
                                                   at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1519) 
                                                   at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240) 

Upvotes: 0

Views: 472

Answers (1)

Yogesh Umesh Vaity
Yogesh Umesh Vaity

Reputation: 48149

If it runs on desktop and doesn't run on Android, there are two possibilities that I can think of:

  1. Check the path of the folder that contains the button2Test.pack file. Android paths are relative to the assets folder while the desktop uses a path relative to the root folder.
  2. Check the case of name of the file button2Test.pack for example whether it should be Button2Test.pack, button2test.PACK or similar comobinations. Because Windows accepts any case files but Android doesn't.

Upvotes: 1

Related Questions