Monday, May 3, 2010

ViewFlipper "Receiver not registered" Error

There is this error that plagued me for a few months. If you use a ViewFlipper, this error will pop up on Android 2.1 devices every once in a while:

java.lang.IllegalArgumentException: Receiver not registered: android.widget.ViewFlipper$1@44b6ab90


It does not affect any devices before 2.1, and so what was troubling at first was that the 2.1 source code wasn't yet released so I could't diagnose the issue. Everyone knew it had to do with orientation changes on an Activity with a ViewFlipper, but why exactly it happens isn't clear (I took my own stab at it, but was incorrect about the exact cause; it just seems to happen randomly).

Thankfully, the source has been available now for a while. Obviously, the problem is that onDetachedFromWindow() is somehow being called before onAttachedToWindow(); but how do we come up with a workaround until it is fixed at Google?

One simple solution is to override onDetachedFromWindow() and catch the error from super:

@Override
protected void onDetachedFromWindow() {
try {
super.onDetachedFromWindow();
}
catch (IllegalArgumentException e) {

}
}


The only problem with this is that the error is thrown before calling updateRunning(). A simple workaround for this is to call stopFlipping(), as that will kick off updateRunning() without any negative side effects:

@Override
protected void onDetachedFromWindow() {
try {
super.onDetachedFromWindow();
}
catch (IllegalArgumentException e) {
stopFlipping();
}
}


You should only use this version of the class on Android 2.1 phones, as they are the only ones requiring the fix.

EDIT May 25, 2010: ViewFlipper bug still occurs in Android 2.2. I sure hope you didn't filter based on apiLevel == 7; better to use apiLevel >= 7.

22 comments:

  1. I've just had this problem as my first bug report in the Market so finding your post has saved me a huge amount of time tracking this down. Thanks for posting this.

    ReplyDelete
  2. Daniel,
    Great to get your blog on this! I am getting this issue in logs from Google for my ap. I will try your solution. However I do not see the reason for applying your fix only if the api is 2.1 or higher. I do not see any negative effect in applying it since you are calling the parent's functions. So is it really necessary?

    ReplyDelete
  3. Thank you very much for this workaround!

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Awesome, just got this error and found this post expecting it to be my fault. Many thanks for the work around!

    ReplyDelete
  6. Just wanted to add my thanks as well. Same problem here of course. Android 2.2.

    Hope to see it fixed in the future.

    ReplyDelete
  7. I agree with all of them, thank you.

    ReplyDelete
  8. Really? Works?!

    Let me check it!

    ReplyDelete
  9. Yes, it reallly works.

    Thank you.

    ReplyDelete
  10. Fixed my annoying issue, thanks a ton.

    ReplyDelete
  11. Thank you! I hope this will fix it... people are getting mad on the market!

    ReplyDelete
  12. Is this fixed in 2.3? Is there a bug report in the database?

    ReplyDelete
  13. http://code.google.com/p/android/issues/detail?id=6191

    ReplyDelete
  14. @Chris I hope you realize I was the one who started that bug. :)

    I was incorrect in that it was not related to TabActivity, however.

    ReplyDelete
  15. Thank you a lot!! You made my day :D

    ReplyDelete
  16. Thank you. You have saved my day :)

    ReplyDelete
  17. This comment has been removed by the author.

    ReplyDelete
  18. i got this exception on android 4.2.1. ??
    can you tell me why this happen .....

    ReplyDelete
  19. yes its error in api provider itself still it continues all levels

    ReplyDelete