The problem is in the message handler in android

I use Handlers in my application on one screen, by clicking a button, a certain set of codes will be called. To call this code set, I send messages to the handler and overrides the descriptor message method. For the first time, when the button is pressed, the handler works fine and the code is typed. When I clicked the button a second time, I get the following exception.

05-03 09:45:25.703: ERROR/AndroidRuntime(1971): FATAL EXCEPTION: main 05-03 09:45:25.703: ERROR/AndroidRuntime(1971): android.util.AndroidRuntimeException: { what=1 when=7381217 obj=android.app.AlertDialog@462b5c58 } This message is already in use. 05-03 09:45:25.703: ERROR/AndroidRuntime(1971): at android.os.MessageQueue.enqueueMessage(MessageQueue.java:171) 05-03 09:45:25.703: ERROR/AndroidRuntime(1971): at android.os.Handler.sendMessageAtTime(Handler.java:457) 05-03 09:45:25.703: ERROR/AndroidRuntime(1971): at android.os.Handler.sendMessageDelayed(Handler.java:430) 05-03 09:45:25.703: ERROR/AndroidRuntime(1971): at android.os.Handler.sendMessage(Handler.java:367) 05-03 09:45:25.703: ERROR/AndroidRuntime(1971): at com.mysnob.utils.MessageDialog$8.onClick(MessageDialog.java:93) 05-03 09:45:25.703: ERROR/AndroidRuntime(1971): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158) 05-03 09:45:25.703: ERROR/AndroidRuntime(1971): at android.os.Handler.dispatchMessage(Handler.java:99) 05-03 09:45:25.703: ERROR/AndroidRuntime(1971): at android.os.Looper.loop(Looper.java:144) 05-03 09:45:25.703: ERROR/AndroidRuntime(1971): at android.app.ActivityThread.main(ActivityThread.java:4937) 05-03 09:45:25.703: ERROR/AndroidRuntime(1971): at java.lang.reflect.Method.invokeNative(Native Method) 05-03 09:45:25.703: ERROR/AndroidRuntime(1971): at java.lang.reflect.Method.invoke(Method.java:521) 05-03 09:45:25.703: ERROR/AndroidRuntime(1971): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 05-03 09:45:25.703: ERROR/AndroidRuntime(1971): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 05-03 09:45:25.703: ERROR/AndroidRuntime(1971): at dalvik.system.NativeStart.main(Native Method) 

I can understand that when sending the same message I get this exception again. But I do not know how to solve this problem, if anyone knows, please help me.

Thanks,

Rajapandian

+6
source share
3 answers

You should never reuse Message obj. Remember the new new Message obj every time you send a message if you need to type the message again and again.

+5
source

There is a helper method that makes a copy of your message. With this, you can send a copy of your original message instead of re-sending the same object (which will not work if the previous one is still in use).

 public static Message obtain (Message orig); 

Others suggest deleting the message from the handler and resending it again. That would solve the exception, but you are unlikely to want it. Deleting and resending may result in the loss of invalid messages. That is why I suggest making a copy of your message.

Check your messages and make sure you do not send them twice.

UPDATE:

And to make it clear ... you can send messages with the same what (or other parameters) as many times as you want. The only thing you need to be sure of is to make a new message every time you send a message. You do not need to delete anything, it will be added to the message handler queue.

-1
source

You need to delete messages from your handler. See my example below: I use this handler to send messages to my dialog (mDialog)

 /** * this property will help send messages to the dialog */ Handler handler = new Handler() { @Override public void handleMessage(Message msg) { mDialog.setMessage((String) msg.obj); removeMessages(0); //this is very important } }; 
-2
source

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


All Articles