I am using Handlers in my application, in one screen by clicking a button some set of codes will be called.To invoke that set of code i am sending messages to the Handler and overridden the handle messages method. First time when clicking the button the handler working perfectly and the set of code is executed. When i clicked the button for the second time i am getting 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 while sending the same message again i am getting this exception. But i don't know how to solve this problem, if anyone knows please help me.
Thanks,
Rajapandian
You should never reuse a Message obj. Remember to new a new Message obj each time you send a message if you need to seed Message again and again.
There is a helper method that makes a copy of your Message. With that you can send the copy of your original Message instead of resending the same object (that would fail if the previous is still being used).
public static Message obtain (Message orig);
Others suggest removing the message from the Handler and resend it again. It would solve the exception, but it is unlikely you would want that. Removing and resending could cause undelivered messages to get lost. That is why I suggest making a copy of your message.
Check your messages, and be sure you don't send any of them twice.
UPDATE:
And to make it clear... you can send messages with the same what
(or other same parameters) as many time as you want. The only thing you have to be sure about is to make new Message every time you send a message. You don't have to remove anything, it will be added to the Handler's message queue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With