google api - java.lang.IllegalStateException: GoogleApiClient is not connected yet -
error message
fatal exception: main java.lang.illegalstateexception: googleapiclient not connected yet. @ com.google.android.gms.internal.zzlg.zzb(unknown source) @ com.google.android.gms.internal.zzli.zzb(unknown source) @ com.google.android.gms.location.internal.zzd.requestlocationupdates(unknown source) @ com.example.argede06.argede.konumservis_konumcek.konum_googleapi.startlocationupdate(konum_googleapi.java:82) @ com.example.argede06.argede.konumservis_konumcek.konum_googleapi$1.run(konum_googleapi.java:62) @ android.os.handler.handlecallback(handler.java:725) @ android.os.handler.dispatchmessage(handler.java:92) @ android.os.looper.loop(looper.java:153) @ android.app.activitythread.main(activitythread.java:5336) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:511) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:833) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:600) @ dalvik.system.nativestart.main(native method)
code
public class konum_googleapi extends service implements connectioncallbacks,onconnectionfailedlistener,locationlistener { int mstartmode; protected googleapiclient mgoogleapiclient; protected locationrequest mlocationrequest; protected location location; private handler mservicehandler; private static final string tag = konum_googleapi.class.getsimplename(); @override public void oncreate() { log.e("buildapi googlclient","sycbhorid"); mgoogleapiclient = new googleapiclient.builder(this) .addconnectioncallbacks(this) .addonconnectionfailedlistener(this) .addapi(locationservices.api) .build(); mgoogleapiclient.connect(); createrequest(); handler handler = new handler(looper.getmainlooper()); handler.post(new runnable() { @override public void run() { startlocationupdate(); } }); handlerthread handlerthread = new handlerthread(tag); handlerthread.start(); mservicehandler = new handler(handlerthread.getlooper()); } protected void createrequest(){ mlocationrequest = new locationrequest(); mlocationrequest.setinterval(0); mlocationrequest.setfastestinterval(0); mlocationrequest.setpriority(locationrequest.priority_high_accuracy); } protected void startlocationupdate(){ log.i(tag, "requesting location updates"); try { locationservices.fusedlocationapi.requestlocationupdates(mgoogleapiclient, mlocationrequest,(com.google.android.gms.location.locationlistener)this); } catch (securityexception unlikely) { log.e(tag, "lost location permission. not request updates. " + unlikely); } } @nullable @override public ibinder onbind(intent intent) { return null; } @override public void onconnected(bundle bundle) { log.e("connect","onconnected"); if(location == null){ location = locationservices.fusedlocationapi.getlastlocation(mgoogleapiclient); } } @override public void onconnectionsuspended(int i) { log.e(tag, "googleapiclient connection suspended."); } @override public void onconnectionfailed(connectionresult connectionresult) { log.e(tag, "googleapiclient connection failed."); } @override public void onlocationchanged(location location) { log.e("(google)kordinatlar =",string.valueof(location.getlongitude())+","+string.valueof(location.getlatitude())); } @override public int onstartcommand(intent intent, int flags, int startid) { super.onstartcommand(intent, flags, startid); return start_sticky; } @override public void ondestroy(){ mservicehandler.removecallbacksandmessages(null); mgoogleapiclient.disconnect(); } }
googleapiclient error in startlocationupdate line protected void
///this eror locationservices.fusedlocationapi.requestlocationupdates(mgoogleapiclient, mlocationrequest,(com.google.android.gms.location.locationlistener)this);
i can not run activiy on service. can not run location update on service. getting error. how can resolve error?
you calling createrequest
before client has connected. mgoogleapiclient.connect();
asynchronous , not connect clien straight away, move createrequest();
onconnected
method way location request sent when api client connected.
Comments
Post a Comment