1

我创造了一些活动和不同的场景。现在我想使用场景管理器将它们全部合并。但是我遇到了 java.lang.RuntimeException 的问题:无法在没有调用 Looper.prepare() 的线程内创建处理程序,因为我使用了处理程序。谁能给我一个快速的解决方案?应该使用哪种机制从分离的类/活动中创建场景管理器?

具体来说:我不能在我的嵌套类中使用处理程序。很抱歉这变成了 Java 问题,而不仅仅是 Andengine。

   public class MainActivity extends BaseGameActivity
   {

    public static class SceneManager1 {

    public static Handler handlerAchievementOwl = new Handler(){
        @Override
          public void handleMessage(Message msg){
            Log.i("Peakpeak","Peak inside Handler");
            }

    };
   }

   }
4

2 回答 2

2

试试这个代码:

public class MainActivity extends BaseGameActivity
{
private static Activity thisActivity;

protected void onCreate(){
    super.onCreate();
   thisActivity = this;
}

public static class SceneManager1 {

    public static Handler handlerAchievementOwl = new Handler(){
        @Override
        public void handleMessage(Message msg){
            Log.i("Peakpeak", "Peak inside Handler");

 thisActivity .runOnUpdateThread(new Runnable() {
                @Override
                public void run() {
                    //do your magic
                }
            });
        }

    };
}
于 2013-07-25T12:07:08.107 回答
0

因为你需要context一个静态类(我不确定你MainActivity是否有context,但我很确定它在某个地方),你可以这样做:

public class MainActivity extends BaseGameActivity
{
    private static Context context;

    protected void onCreate(){
        super.onCreate();

        MainActivity.context = getApplicationContext();
    }

    public static class SceneManager1 {

        public static Handler handlerAchievementOwl = new Handler(){
            @Override
            public void handleMessage(Message msg){
                Log.i("Peakpeak", "Peak inside Handler");

                ((Activity)MainActivity.context).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //do your magic
                    }
                });
            }

        };
    }

}
于 2013-07-25T11:38:56.470 回答