1

I made an app, that works good, but something strange happens. When you install the app for the 1st time, open it and put onPause, lock your phone and then you resume it clicking on icon, when it is loaded, click on BACK button (Gdx.app.exit();), It gives me the following exception:

E/AndroidRuntime: FATAL EXCEPTION: GLThread 1723
       java.lang.NullPointerException
       at com.badlogic.gdx.Game.render(Game.java:46)
       at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(AndroidGraphics.java:459)
       at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1524)
       at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1248)

After you launch the app again, and it works properly. So, trying to figure out this error, I made a new project with gdx-setup.jar. I didn't change AndroidLauncher, neither gradle. Only in manifest I changed landscape to portrait. And I have only 4 classes with almost no code and the problem is still there. May be somebody know what happens. I will appreciate any help. Thanks!

My code:

Main class.

public class TenPuzzle extends Game {

    @Override
    public void create () {

        setScreen(new SplashScreen(this));
    }

}

SpalashScreen.java

 public class SplashScreen extends Screens{

    public SplashScreen(TenPuzzle ten) {
        super(ten);

        Gdx.app.log("1010", "SPLASH");

    }

    @Override
    public void draw(float delta) {
        game.setScreen(new MainMenuScreen(game));

    }

    @Override
    public void update(float delta) {

    }

}

MainMenuScreen.java

 public class MainMenuScreen extends Screens {


    public MainMenuScreen(TenPuzzle ten) {
        super(ten);

        Gdx.app.log("1010", "Main menu screen");
    }

    @Override
    public void draw(float delta) {}

    @Override
    public void update(float delta) {}

    public boolean keyDown(int keycode) {

        if(keycode == Input.Keys.BACK){
                Gdx.app.exit();
        }
        return false;
    }

}

and Screens.java

public abstract class Screens extends InputAdapter implements Screen {

    public static float SCREEN_WIDTH = 575;
    public static float SCREEN_HEIGHT = 1024;

    public TenPuzzle game;

    public OrthographicCamera oCam;
    public SpriteBatch batcher;
    public Stage stage;
    public static float diffViewportStage;
    public Screens(TenPuzzle game) {
        this.game = game;
        stage = new Stage(new ExtendViewport(Screens.SCREEN_WIDTH,
                    Screens.SCREEN_HEIGHT, 768, 1224));

        oCam = new OrthographicCamera(stage.getWidth(), stage.getHeight());
            oCam.position.set(stage.getWidth()/ 2,stage.getHeight()/2f, 0);


        InputMultiplexer input = new InputMultiplexer(this, stage);
        Gdx.input.setInputProcessor(input);
        Gdx.input.setCatchBackKey(true);

    }

    @Override
    public void render(float delta) {

        update(delta);
        oCam.update();
        stage.act(delta);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        draw(delta);
        stage.draw();

    }

    public abstract void draw(float delta);

    public abstract void update(float delta);

    @Override
    public void resize(int width, int height) {

        stage.getViewport().update(width, height, true);

    }

    @Override
    public void show() {
    }

    @Override
    public void hide() {

    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {

    }

    @Override
    public void dispose() {
        stage.dispose();

    }

    @Override
    public boolean keyDown(int keycode) {
        return super.keyDown(keycode);
    }


}
ketty
  • 21
  • 5
  • Welcome to StackOverflow. First, you have several extra `}` in your post. Are those here by mistake or your code has some of those? Please correct that either way. Second, your code is still somewhat long. Have you tried to reduce it further? For instance, by removing empty functions. I think you will bring more attention to your question if you reduce the size further (according to http://stackoverflow.com/help/mcve). Best regards. – YakovL Jun 26 '16 at 17:36
  • @YakovL Ok, I checked all {}, it was a mistake while posting. And have cleaned the code a bit. Anyway, there are almost nothing, and I still have this error. – ketty Jun 26 '16 at 20:42

1 Answers1

1

Ok, I found the solution. I have no problem in my code, the problem was the Android bug described here: Re-launch of Activity on Home button, but...only the first time

Community
  • 1
  • 1
ketty
  • 21
  • 5