multithreading - android, error when create a thread in onCreate() -
i want long operation (like copying/loading file) when application created. created thread so, thread not update ui. got error saying cannot create handler in thread without calling looper.prepare(). what's wrong code?
public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); threadfilemanager = new thread ( new runnable() { public void run() { filemanager fm = new filemanager(); fm.copyfilefromassettostorage(); } }); threadfilemanager.start(); }
edit:the error lied in filemanager class, when subclass of activity. changing service worked.
the application
class wrong place this. if want, it's acceptable use application
's oncreate()
method start service
. should implement background thread in service
, purpose of service
things in background. application
class should seldom used. it's last resort maintaining minimal amounts of global state.
once move code service
, looper.prepare()
have been called android.
edit:
op trying create handler inside of thread when invoked constructor of filemanager class. while comment above still true, not relevant op's question subclassing activity , not application.
to clear, actual problem was creating handler inside thread had not yet called looper.prepare() (via new filemanager()). correct fix create handler on main thread, i.e. in 1 of activity or service callbacks.
Comments
Post a Comment