java - Can't create handler inside thread that has not called Looper.prepare() - Android Marmalade -


i'm trying add usb controller support android game. i'm using marmalade , i've created extension based on usb example code. here is:

public class gamecontrollerinput extends activity              implements inputmanager.inputdevicelistener  {     private static final string tag = "gamecontrollerinput";      private inputmanager minputmanager;     private sparsearray<inputdevicestate> minputdevicestates;      private static int numevents = 0;       public int edk_gamecontrollerinput_init()     {         loaderactivity.m_activity.runonuithread(new runnable()          {         public void run()         {             log.i(tag, "running 1 =========================");         }     });     log.i(tag, "init 2 =========================");     return 1; 

when call init function error:

java.lang.runtimeexception: can't create handler inside thread has not called looper.prepare() 

i've read other threads error , solution add loaderactivity.m_activity.runonuithread(new runnable() code. however, can see, adding gives me same error.

i'm not experienced java , i'm @ loss on how fix this. appreciated.

cheers, steve

a looper (a message queue processor) tied single thread, each thread has @ 1 looper. handler needs register looper work, each time invoke new handler(), tries looper current thread (the thread that's creating handler), can either present or not. exception see thrown because thread that's creating handler not have looper.

there 1 2 things can fix this:

  • add looper current thread.
  • make sure you're creating handler on thread has looper.

in cases, handler used communicate background thread ui thread, i'm assuming that's case here. means option 2. runonuithread(runnable) thing close, no cigar, because write log file.

you need move code creates new handler() (not shown in posted code sample) runonuithread block, or use other way run on ui thread. typical way create in oncreate(bundle) method of activity or fragment.

keep in mind that, depending on initialization order, may mean it's null seen background thread, background code have able deal that.


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -