You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio:libgdx scene2d的InputMultiplexer在Release模式崩溃

问题:Gradle升级至8.1.1后Release版本点击屏幕崩溃
  • 现有一套已投入使用的游戏框架(多款应用基于该框架开发),在Gradle 7.4.2版本下,Debug和Release两种构建变体均能正常运行;升级至Gradle 8.1.1后,仅Debug版本可用,Release版本点击屏幕(真机或模拟器)会立即崩溃。
  • 若注释掉BaseScreen的show()方法中与InputMultiplexer相关的代码,Release版本即可恢复正常运行。
  • 另外发现:将构建变体从Debug切换至Release时,会弹出一个对话框,多次点击顶部的“选择模块”后错误提示消失,不确定这是否是问题的根源。

相关代码

BaseGame类

public abstract class BaseGame extends Game
{
    public static TextButton.TextButtonStyle textButtonStyle;
    /**
     *  Stores reference to game; used when calling setActiveScreen method.
     */
    private static BaseGame game;

    /**
     *  Called when game is initialized; stores global reference to game object.
     */
    public BaseGame() 
    {        
        game = this;
    }
    
    /**
     *  Called when game is initialized, 
     *  after Gdx.input and other objects have been initialized.
     */
    public void create() 
    {        
       // prepare for multiple classes/stages/actors to receive discrete input
        InputMultiplexer im = new InputMultiplexer();
        Gdx.input.setInputProcessor( im );
    }
    
    /**
     *  Used to switch screens while game is running.
     *  Method is static to simplify usage.
     */
    public static void setActiveScreen(BaseScreen s)
    {
        game.setScreen(s);
    }

    @Override
    public void dispose() {

        //wird nur hier Automatisch aufgerufen!!!!!!!!!!!!!!!
        screen.dispose();

    }
}

BaseScreen类

public abstract class BaseScreen implements Screen, InputProcessor
{
    protected Stage mainStage;
    protected Stage uiStage;
    Viewport viewport;

    public BaseScreen()
    {
        mainStage = new Stage(viewport=new ExtendViewport(380,950));
        uiStage = new Stage(viewport=new ExtendViewport(380,950));

        initialize();
    }

    public abstract void initialize();

    public abstract void update(float dt);


    public void render(float dt)
    {
        // act methods
        uiStage.act(dt);
        mainStage.act(dt);

        // clear the screen
        Gdx.gl.glClearColor(255,0,0,1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        
        // draw the graphics
        mainStage.draw();
        uiStage.draw();
        // defined by user
        update(dt);

    }

    // methods required by Screen interface
    public void resize(int width, int height) {
        mainStage.getViewport().update(width, height);
        uiStage.getViewport().update(width, height);
    }

    public void pause()   {  }

    public void resume()  {  }

    public void dispose() {

    }

     /**
     *  Called when this becomes the active screen in a Game.
     *  Set up InputMultiplexer here, in case screen is reactivated at a later time.
     */
    public void show()    
    {
        InputMultiplexer im = (InputMultiplexer)Gdx.input.getInputProcessor();
        im.addProcessor(this);
        im.addProcessor(uiStage);
        im.addProcessor(mainStage);
    }

    /**
     *  Called when this is no longer the active screen in a Game.
     *  Screen class and Stages no longer process input.
     *  Other InputProcessors must be removed manually.
     */
    public void hide()    
    {  
        InputMultiplexer im = (InputMultiplexer)Gdx.input.getInputProcessor();
        im.removeProcessor(this);
        im.removeProcessor(uiStage);
        im.removeProcessor(mainStage);
    }
    
    // methods required by InputProcessor interface
    public boolean keyDown(int keycode)
    {  return false;  }

    public boolean keyUp(int keycode)
    {  return false;  }

    public boolean keyTyped(char c) 
    {  return false;  }

    public boolean mouseMoved(int screenX, int screenY)
    {  return false;  }

    public boolean scrolled(int amount) 
    {  return false;  }

    public boolean touchDown(int screenX, int screenY, int pointer, int button) 
    {  return false;  }

    public boolean touchDragged(int screenX, int screenY, int pointer) 
    {  return false;  }

    public boolean touchUp(int screenX, int screenY, int pointer, int button)
    {  return false;  } 
}

SpaceGame类

public class SpaceGame extends BaseGame
{
    public void create() 
    {        
        super.create();
        setActiveScreen( new Start() );
    }
} 

内容的提问来源于stack exchange,提问作者HermannGo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 17:13:16