5

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

6
  • Are you calling removeMessages(what) in the handler and Message.obtainMessage(what) to obtain the message? Commented May 3, 2011 at 4:53
  • @JAL I am getting the message using Message.obtainMessage(), i checked the Message.obtainMessage(what) but i does not found any method like this. Commented May 4, 2011 at 4:07
  • From the docs obtainMessage(int what) Same as obtainMessage(), except that it also sets the what member of the returned Message. Link: developer.android.com/reference/android/os/… Commented May 4, 2011 at 6:17
  • possible duplicate of Android Handler Message and ListView Commented Dec 29, 2013 at 10:30
  • removeMessages(what) is not necessary with obtainMessage(what) Commented May 27, 2014 at 13:20

4 Answers 4

11

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.

Sign up to request clarification or add additional context in comments.

1 Comment

absolutely right :). we can use a Message reference once only.
1

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.

2 Comments

I was making a copy with this code Message message = Message.obtain(handler); message.obj = "preparing..."; handler.sendMessage(message); But you will still get the same error: (This message is already in use)
I have done this a thousand of times, and I tested it right now. It works. You must have messed up smth.
0

Try to Create New instance of Message Every Time, Even you could send the same Data, but just create the message instance new, for everytime. Show the problem of in use not create. Example :

Bundle bundle = new Bundle();
bundle.putString("SahittoEntryAddedDirect", "SahittoEntryAddedDirect");

builder.setPositiveButton("Finalize Adding", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
        Message msg = new Message();
        msg.setData(bundle);
        ReadBooksList.messageHandler.sendMessage(msg);
    }
});

               
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
    @Override
    public void onCancel(DialogInterface dialogInterface) {
        Message msg = new Message();
        msg.setData(bundle);
        ReadBooksList.messageHandler.sendMessage(msg);
    }
});

dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
    @Override
    public void onDismiss(DialogInterface dialogInterface) {
        Message msg = new Message();
        msg.setData(bundle);
        ReadBooksList.messageHandler.sendMessage(msg);
    }
});

           

Comments

-2

You need to remove 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
    }
};

1 Comment

You don't have to remove the message, because the OS will do it for you. Its completely automatical.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.