Search code examples
javalibgdx

When trying to create multiple screens in Libgdx when using Java I get a com.mygdx.game.MyGdxGame cannot be cast to com.badlogic.gdx.Game error


Okay so I'm trying to do something like this:

How to create libgdx main menu screen

but when I try to set by one screen like this:

    ((Game)Gdx.app.getApplicationListener()).setScreen(new TestScreen());

I get this error

       Exception in thread "LWJGL Application" java.lang.ClassCastException: com.mygdx.game.MyGdxGame cannot be cast to com.badlogic.gdx.Game
at com.mygdx.game.MyGdxGame.create(MyGdxGame.java:22)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:149)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:126)

I'm not sure what is causing it or how to fix it. Anyone had the same issue and know a solution? If so thank you in advanced.

Edit: The stack overflow post I linked to wasn't the one I originally wanted to link to so I changed it


Solution

  • Turns out I had to set my game to extend Game instead of extending ApplicaitonAdapter. Don't Trust every tutorial you read online haha.

    Now I have a different problem, but this one is solved.