2017-10-09 52 views
0

IM加吐司試圖創建與接口 接口稱之爲吐司工作正常,只是,當我嘗試敬酒它,它崩潰我不能在libgdx

在Android啓動

public class AndroidLauncher extends AndroidApplication implements Interface {  
public void tost() { 
     Toast.makeText(getContext(), "wrong num", 
       Toast.LENGTH_LONG).show();}} 

在gameclass

interface Interface{void tost();} 
    public class MyGdxGame extends ApplicationAdapter { 
     final private Interface interface; 
     public MyGdxGame(Interface interface){this.interface=interface;} 
    public void render() {interface.tost} 

回答

0

Android的祝酒詞必須在Android的UI線程上執行。 LibGDX中的遊戲循環運行在OpenGL線程上,因此您必須將您的方法發佈到UI線程才能安全地運行它,如下所示:

public class AndroidLauncher extends AndroidApplication implements Interface {  

    public void tost() { 
     runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       Toast.makeText(getContext(), "wrong num", 
        Toast.LENGTH_LONG).show();} 
      } 
     }); 
    } 

}