A thread that intends to spend its lifetime in a message dispatching loop needs to call Looper.prepare() before going into Looper.loop().
http://developer.android.com/reference/android/os/Looper.html This initializes the data structures needed by loop() later. The UI thread has this done by the framework, but it's necessary for your own thread. -- Kostya 30 ноября 2011 г. 18:15 пользователь John Goche <[email protected]>написал: > > Thank you for your reply, I've restructured my code a little bit > and now I do have multiple handlers in my code but no longer > get the error message about having to call looper.prepare() > (which I tried calling without understanding exactly what it > does and didn't get a nice result). Sorry if I left the details > out, I spent some time restructuring my code and then was > unable to get the same error message hence the vagueness. > > I'm still interested in what might have caused the orignal message > telling me to call looper.prepare() though and the logic behind it. > > Thanks, > > John Goche > > > 2011/11/30 Kostya Vasilyev <[email protected]> > >> You can have as many handlers as needed, within reason. >> >> The work of dispatching messages to handlers is done by Looper, anyway. >> >> And that's where I think your original issue has its roots. >> >> Posting a more exact message than "I get an error saying I need to >> >> call prepare *or something* since I cannot have more than one >> handler per thread *or something*." >> >> ( I am pretty sure what it is, but let's see the message first ) >> >> -- Kostya >> >> 30 ноября 2011 г. 16:35 пользователь John Goche < >> [email protected]> написал: >> >> >>> Also, why would it be better to have one handler than multiple handlers >>> anyways >>> given that in some situations multiple handlers can compile? >>> >>> >>> On Wed, Nov 30, 2011 at 1:32 PM, John Goche >>> <[email protected]>wrote: >>> >>>> >>>> Thank you TreKing for your advice, >>>> >>>> But then in my handleMessage I would have to check with >>>> >>>> if (inst instance of Foo) >>>> inst.foocallback(); >>>> else if (inst instanceof Bar) >>>> inst.barcallback(); >>>> else if (...) >>>> >>>> or is there a neater solution, perhaps using polymorphism or a design >>>> pattern of some sort >>>> so that my handleMessage does not quickly turn into a mess? >>>> >>>> Thanks, >>>> >>>> John Goche >>>> >>>> >>>> >>>> On Tue, Nov 29, 2011 at 11:02 PM, TreKing <[email protected]> wrote: >>>> >>>>> On Tue, Nov 29, 2011 at 3:46 PM, John Goche < >>>>> [email protected]> wrote: >>>>> >>>>>> The number of sprites is indefinite (could vary in number) but each >>>>>> needs to manage itself individually. On top of this the world has its >>>>>> own handler to manage updates to itself. >>>>>> >>>>> >>>>> You don't need separate handlers, you can pass the instance of the >>>>> object that is being updated in the handler message, then use one single >>>>> handler for the main thread. >>>>> >>>>> >>>>> ------------------------------------------------------------------------------------------------- >>>>> TreKing <http://sites.google.com/site/rezmobileapps/treking> - >>>>> Chicago transit tracking app for Android-powered devices >>>>> >>>>> -- >>>>> You received this message because you are subscribed to the Google >>>>> Groups "Android Developers" group. >>>>> To post to this group, send email to >>>>> [email protected] >>>>> To unsubscribe from this group, send email to >>>>> [email protected] >>>>> For more options, visit this group at >>>>> http://groups.google.com/group/android-developers?hl=en >>>> >>>> >>>> >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "Android Developers" group. >>> To post to this group, send email to [email protected] >>> To unsubscribe from this group, send email to >>> [email protected] >>> For more options, visit this group at >>> http://groups.google.com/group/android-developers?hl=en >>> >> >> -- >> You received this message because you are subscribed to the Google >> Groups "Android Developers" group. >> To post to this group, send email to [email protected] >> To unsubscribe from this group, send email to >> [email protected] >> For more options, visit this group at >> http://groups.google.com/group/android-developers?hl=en >> > > -- > You received this message because you are subscribed to the Google > Groups "Android Developers" group. > To post to this group, send email to [email protected] > To unsubscribe from this group, send email to > [email protected] > For more options, visit this group at > http://groups.google.com/group/android-developers?hl=en > -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

