How to show warning dialog in current thread?

I am developing an Android game. This game has tracks on which trains run. This works stream. I want to show a warning dialog when there is a collision between them. when I apply a warning dialog showing an error, it cannot create a handler inside the thread that looper.prepare() did not call.

+4
source share
5 answers

You will need to create an AlertDialog inside the UI thread, otherwise it will never work. If you are using another thread, use MessageHandler or you can use runOnUiThread (using runnable) to create a dialog inside.

+11
source

This will help you:

 runOnUiThread(new Runnable() { @Override public void run() { // Your dialog code. } }); 
+11
source

You can create a handler in the Activity class and call sendMessage for this handler object. Write code to display a warning in the handleMessage method of the handler, for example:

Action class

 Handler mHandler = new Handler() { public void handleMessage(Message msg) { //Display Alert } }; //Thread Thread thread= new Thread() { public void run() { //Logic MHandler.sendEmptyMessage(0); } } 
+6
source

You can use handlers to do this work.

 Handler mHandler = new Handler(Looper.getMainLooper()); mHandler.post(new Runnable() { @Override public void run() { // Your UI updates here } }); 
+4
source

You should show your dialog box in the user interface stream as shown below

 runOnUiThread(new Runnable() { @Override public void run() { // Your dialog code. } }); 
+1
source

Source: https://habr.com/ru/post/1433943/


All Articles