Toasts in AndEngine Scenes

試著忘記壹切 提交于 2019-12-25 08:56:41

问题


I am using a scene manage and I have different classes extended from scene which are used to to display different modes. I am getting the prob in Toasting messages. In my Mode1 class extended from scene I want to show a toast message but it gives error that "Cant create handler inside thread that has not called looper.prepare()"

I tried to do this inside handler but same result. I tried to make a static handler in main class but no result. Can any one tell me the solution?


回答1:


You can create toast message using following way also.

mainActivity.toastOnUiThread("No moves available for REDO",
                Toast.LENGTH_SHORT);



回答2:


The key is to run it on the UI Thread - this is what I use

    public void gameToast(final String msg) {
    this.runOnUiThread(new Runnable() {
        @Override
        public void run() {
           Toast.makeText(MyMainActivity.this, msg, Toast.LENGTH_SHORT).show();
        }
    });
}



回答3:


in your class extending from BaseGameActivity just make a method like

public static void MakeToast(String Msg)
{
    message = Msg;
    Handles.sendEmptyMessage(0);
}

static Handler Handles = new Handler()
{
    public void handleMessage(android.os.Message msg) {

        if(msg.what==0)
        {
            Toast.makeText(myCxt, message, Toast.LENGTH_SHORT).show();
        }
    };
};

and in your scene class you will call. YourBaseGameActivity.MakeToast("Hello World");

message is a static String variable too.



来源:https://stackoverflow.com/questions/9849741/toasts-in-andengine-scenes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!