java - Extending HandlerThread and when to call getLooper() -


i have simple activity button on it. upon button's onclick method, want run code in thread, getting handlerthread's looper object , posting runnable handler associated it.
objective call thread's method thread (and run own thread) using handlers (probably bad idea, wanted learn instead of abusing static methods)

decided extend handlerthread. new class not much.

public class epicthread extends handlerthread {     public epicthread(string name) {         super(name);     } } 

the activity not either. did testing.

public class mainactivity extends activity {     epicthread et;     looper l;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          epicthread et = new epicthread("the epic thread");         et.start();     }      public void click(view v) {         l = et.getlooper();         handler h = new handler(l);         h.post(new runnable() {             @override             public void run() {                 system.out.println("this running on epic thread");             }         });     } } 

so test pressing button, gives me invocationtargetexception when getlooper() called:

e/androidruntime(23262): fatal exception: main e/androidruntime(23262): java.lang.illegalstateexception: not execute method of activity e/androidruntime(23262):    @ android.view.view$1.onclick(view.java:3660) e/androidruntime(23262):    @ android.view.view.performclick(view.java:4162) e/androidruntime(23262):    @ android.view.view$performclick.run(view.java:17088) e/androidruntime(23262):    @ android.os.handler.handlecallback(handler.java:615) e/androidruntime(23262):    @ android.os.handler.dispatchmessage(handler.java:92) e/androidruntime(23262):    @ android.os.looper.loop(looper.java:137) e/androidruntime(23262):    @ android.app.activitythread.main(activitythread.java:4867) e/androidruntime(23262):    @ java.lang.reflect.method.invokenative(native method) e/androidruntime(23262):    @ java.lang.reflect.method.invoke(method.java:511) e/androidruntime(23262):    @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1007) e/androidruntime(23262):    @ com.android.internal.os.zygoteinit.main(zygoteinit.java:774) e/androidruntime(23262):    @ dalvik.system.nativestart.main(native method) e/androidruntime(23262): caused by: java.lang.reflect.invocationtargetexception e/androidruntime(23262):    @ java.lang.reflect.method.invokenative(native method) e/androidruntime(23262):    @ java.lang.reflect.method.invoke(method.java:511) e/androidruntime(23262):    @ android.view.view$1.onclick(view.java:3655) e/androidruntime(23262):    ... 11 more e/androidruntime(23262): caused by: java.lang.nullpointerexception e/androidruntime(23262):    @ com.asdf.qwert.mainactivity.click(mainactivity.java:49) e/androidruntime(23262):    ... 14 more 

fiddling little code, noticed :

  • getting looper l = et.getlooper(); works right after creating thread, after et.start();.
  • if use handlerthread directly instead of epicthread, works regardless of when getlooper() called.

so have no idea why exception thrown, , why not using subclass things started work.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -