开发者

problem in Handler with messages in android

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(ActivityThrea开发者_如何学Pythond.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.


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);
    }
});

           


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
    }
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜