android - AsyncTask and Looper.prepare() error -
i have following code
class overlaytask extends asynctask<void, void, void> { @override public void onpreexecute() { if (sites != null) { mymapview.getoverlays().remove(sites); mymapview.invalidate(); sites = null; } } @override public void doinbackground(void... unused) { grabshipswithlocation(); return (null); } @override public void onpostexecute(void unused) { mymapview.getoverlays().add(sites); mymapview.invalidate(); isloading = false; } }
that seems work fine on few test devices seeing lot of errors appearing on dev console. can't seem work out why , put looper.prepare(). needed?
java.lang.exceptionininitializererror @ com.test.appname.findermain$1.gotlocation(findermain.java:286) @ com.test.appname.mylocation$getlastlocation.run(mylocation.java:89) @ java.util.timer$timerimpl.run(timer.java:289) caused by: java.lang.runtimeexception: can't create handler inside thread has not called looper.prepare() @ android.os.handler.<init>(handler.java:121) @ android.os.asynctask$internalhandler.<init>(asynctask.java:421) @ android.os.asynctask$internalhandler.<init>(asynctask.java:421) @ android.os.asynctask.<clinit>(asynctask.java:152)
as requested mylocation.java
class getlastlocation extends timertask { @override public void run() { lm.removeupdates(locationlistenergps); lm.removeupdates(locationlistenernetwork); location net_loc=null, gps_loc=null; if(gps_enabled) gps_loc=lm.getlastknownlocation(locationmanager.network_provider); if(network_enabled) net_loc=lm.getlastknownlocation(locationmanager.gps_provider); //if there both values use latest 1 if(gps_loc!=null && net_loc!=null){ if(gps_loc.gettime()>net_loc.gettime()) locationresult.gotlocation(gps_loc); else locationresult.gotlocation(net_loc); return; } if(gps_loc!=null){ locationresult.gotlocation(gps_loc); //line 89 return; } if(net_loc!=null){ locationresult.gotlocation(net_loc); return; } locationresult.gotlocation(null); } }
long story:
asynctask
internally uses handler
. handler allows post runnables
thread on thread handler assigned to, in case of asynctask
thread called. works threads have looper
prepared, though.
for more information see http://developer.android.com/reference/android/os/handler.html
short story:
simply wrap every call findermain$1.gotlocation
or creation of asynctask
within in runnable
, , post handler
bound ui thread, this:
class getlastlocation extends timertask { private handler mhandler = new handler(looper.getmainlooper()); @override public void run() { // ... mhandler.post(new runnable() { public void run() { locationresult.gotlocation(null); } }); // ... } }
Comments
Post a Comment