java - android error "cant create handler inside thread that has not called looper.prepare" -
this question has answer here:
i have dialog fragment named shareid , service named sendmapdataservice.when called sendmapdataservice shareid got error cant create handler inside thread has not called looper.prepare....
shareid
package dialog; import com.rosaloves.bitlyj.url; import static com.rosaloves.bitlyj.bitly.*; import java.util.calendar; import updatedata.gpstracker; import updatedata.sendmapdataservice; import com.example.map.locationsendactivity; import com.example.map.r; import android.app.progressdialog; import android.content.intent; import android.net.uri; import android.os.asynctask; import android.os.bundle; import android.support.v4.app.dialogfragment; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.button; import android.widget.edittext; import android.widget.toast; public class shareid extends dialogfragment { button ok,cancel; public static edittext et; private double latt,lngt; public static string sid,lat,lng,time; progressdialog pdialog; gpstracker gps; public void oncreate(bundle savedinstancestate){ super.oncreate(savedinstancestate); } public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view v = inflater.inflate(r.layout.shareid, container, false); ok=(button)v.findviewbyid(r.id.button1); cancel=(button)v.findviewbyid(r.id.button2); et=(edittext)v.findviewbyid(r.id.edittext1); getdialog().settitle("enter share_id"); ok.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub sid=et.gettext().tostring(); if(sid.length() == 0 ){ toast.maketext(getactivity(),"please enter share_id",toast.length_short).show(); }else{ new sharetask().execute(); } } }); cancel.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub getdialog().dismiss(); } }); return v; } class sharetask extends asynctask<string, string, string>{ stringbuffer sbf2=new stringbuffer(); boolean b=false; int ch; protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(getactivity()); pdialog.setmessage("loading application. please wait..."); pdialog.setindeterminate(false); pdialog.setcancelable(false); pdialog.show(); } @override protected string doinbackground(string... params) { // todo auto-generated method stub // create download url url url = as("adeveloper962", "r_38e071119c3048db892f3aca82b03392") .call(shorten("http://map.rhaasoft.net//locationfile/download.php")); string ul = url.getshorturl(); sbf2.append("click download locationmapping file"+" "+ul+"\n"); //create location url gps = new gpstracker(getactivity()); if(gps.cangetlocation()){ log.e("shareid", "hello"); b=true; //get time time= java.text.dateformat.getdatetimeinstance().format(calendar.getinstance().gettime()); //get latitude longitude latt = gps.getlatitude(); lat=string.valueof(latt); lngt= gps.getlongitude(); lng=string.valueof(lngt); //add link uri.builder ur=uri.parse("http://myapp.com/?").buildupon(); ur.appendqueryparameter("la", lat); ur.appendqueryparameter("ln", lng); ur.appendqueryparameter("ti", time); ur.appendqueryparameter("id", sid); ur.build(); //shorting url url url1 = as("adeveloper962", "r_38e071119c3048db892f3aca82b03392") .call(shorten(ur.tostring())); string ul1 = url1.getshorturl(); //append sbf2.append("click show location"+" "+ul1); }else{ b=false; } return null; } protected void onpostexecute(string file_url) { pdialog.dismiss(); getactivity().runonuithread(new runnable() { public void run() { if(b == true){ getdialog().dismiss(); intent intent = new intent(intent.action_send); intent.settype("text/plain"); intent.putextra(intent.extra_subject, "location shareing"); intent.putextra(intent.extra_text, sbf2.tostring()); startactivity(intent.createchooser(intent, "select application share")); //send continue data ch=locationsendactivity.state; if(ch == 0 || ch ==1){ locationsendactivity.i=1; locationsendactivity.u_id=sid; getactivity().startservice(new intent(getactivity(),sendmapdataservice.class)); }else{ locationsendactivity.i=0; getactivity().stopservice(new intent(getactivity(),sendmapdataservice.class)); } }else{ getdialog().dismiss(); gps.showsettingsalert(); } } }); } } }
sendmapdataservice..
package updatedata; import java.util.calendar; import org.json.jsonexception; import org.json.jsonobject; import com.example.map.functioncall; import com.example.map.locationsendactivity; import android.app.service; import android.content.intent; import android.os.asynctask; import android.os.handler; import android.os.ibinder; import android.os.looper; import android.util.log; import android.widget.toast; public class sendmapdataservice extends service { private handler handler; private string id1,lat1,lng1,time1; private double lat,lng; gpstracker gps; private long total,val,min; @override public ibinder onbind(intent intent) { // todo auto-generated method stub gps=new gpstracker(getapplicationcontext()); return null; } public int onstartcommand(intent intent, int flags, int startid) { id1=locationsendactivity.u_id; // log.e("myid", string.valueof(locationsendactivity.u_id)); // time time interval if(min == 1){ val=15; }else if(min == 2){ val=30; }else if(min == 3){ val=60; }else if(min == 4){ val=5*60; }else if(min == 5){ val=10*60; } else{ val=15; } total=(long) (val*1000); // log.e("seconds", string.valueof(total)); // start handler handler=new handler(); handler.postdelayed(runn, 1000); // looper.loop(); return start_sticky; } runnable runn = new runnable() { @override public void run() { gps=new gpstracker(getapplicationcontext()); if(gps.cangetlocation()){ lat = gps.getlatitude(); lng = gps.getlongitude(); time1=java.text.dateformat.getdatetimeinstance().format(calendar.getinstance().gettime()); lat1=string.valueof(lat); lng1=string.valueof(lng); new sendupdateddata(id1,lat1,lng1,time1).execute(); handler.postdelayed(this, total); }else{ gps.showsettingsalert(); } } }; public void ondestroy() { super.ondestroy(); handler.removecallbacks(runn,null); toast.maketext(this, "data send service destroyed", toast.length_long).show(); } class sendupdateddata extends asynctask<string, string, string>{ string id2,lat2,lng2,time2; int success,val,update; public sendupdateddata(string id, string lat,string lng,string time) { // todo auto-generated constructor stub this.id2=id; this.lat2=lat; this.lng2=lng; this.time2=time; } protected void onpreexecute(){ super.onpreexecute(); } @override protected string doinbackground(string... params) { // todo auto-generated method stub functioncall fc=new functioncall(); jsonobject jo=fc.sendupdatedata(id2,lat2,lng2,time2); try{ success=jo.getint("success"); update=jo.getint("update"); }catch(jsonexception e){ e.printstacktrace(); } return null; } protected void onpostexecute(string file_url){ if (success==1){ if(update == 1){ toast.maketext(getapplication(),"data updated", toast.length_short).show(); }else{ toast.maketext(getapplication(),"new data inserted", toast.length_short).show(); } } else{ toast.maketext(getapplication(),"data not send", toast.length_short).show(); } } } }
logcat....
02-06 12:16:43.740: w/system.err(15622): java.lang.runtimeexception: can't create handler inside thread has not called looper.prepare() 02-06 12:16:43.740: w/system.err(15622): @ android.os.handler.<init>(handler.java:121) 02-06 12:16:43.740: w/system.err(15622): @ android.location.locationmanager$listenertransport$1.<init>(locationmanager.java:137) 02-06 12:16:43.750: w/system.err(15622): @ android.location.locationmanager$listenertransport.<init>(locationmanager.java:137) 02-06 12:16:43.750: w/system.err(15622): @ android.location.locationmanager._requestlocationupdates(locationmanager.java:708) 02-06 12:16:43.750: w/system.err(15622): @ android.location.locationmanager.requestlocationupdates(locationmanager.java:630) 02-06 12:16:43.750: w/system.err(15622): @ updatedata.gpstracker.getlocation(gpstracker.java:65) 02-06 12:16:43.750: w/system.err(15622): @ updatedata.gpstracker.<init>(gpstracker.java:44) 02-06 12:16:43.750: w/system.err(15622): @ dialog.shareid$sharetask.doinbackground(shareid.java:96) 02-06 12:16:43.750: w/system.err(15622): @ dialog.shareid$sharetask.doinbackground(shareid.java:1) 02-06 12:16:43.750: w/system.err(15622): @ android.os.asynctask$2.call(asynctask.java:185) 02-06 12:16:43.750: w/system.err(15622): @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:305) 02-06 12:16:43.750: w/system.err(15622): @ java.util.concurrent.futuretask.run(futuretask.java:137) 02-06 12:16:43.750: w/system.err(15622): @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1068) 02-06 12:16:43.750: w/system.err(15622): @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:561) 02-06 12:16:43.750: w/system.err(15622): @ java.lang.thread.run(thread.java:1096)
just call looper.prepare();
above line got exception..
Comments
Post a Comment