android - Journey mornitoring - stop working when adding marker to map -
my location app ok. purpose change app journey monitoring. idea getting information website containing current latitude , longitude of vehicle. added url request runnable. received java.lang.illegalstateexception: not on main thread, com.google.android.gms.maps.internal.igooglemapdelegate$a$a.addmarker(unknown source) , com.google.android.gms.maps.googlemap.addmarker(unknown source). how can solve this.
activity:
package tri.mylocapp; //import... public class mainactivity extends activity{ googlemap googlemap = null; latlng latlng; locationmanager lm; string provider; location location; markeroptions marker; double lat,lng; url url; urlconnection conn; string str; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); if (isgoogleplay()){ setcontentview(r.layout.activity_main); setupmap(); } } private boolean isgoogleplay(){ int status = googleplayservicesutil.isgoogleplayservicesavailable(this); if (status == connectionresult.success){ log.d("mytag", "google ok"); return true; } else{ log.d("mytag","google not ok"); toast.maketext(this, "googleplay not available", toast.length_short).show(); } return false; } private void setupmap(){ if (googlemap == null){ log.d("mytag", "googlemap null, making available"); googlemap = ((mapfragment)getfragmentmanager().findfragmentbyid(r.id.map)).getmap(); if (googlemap != null){ log.d("mytag", "googlemap not null"); googlemap.setmylocationenabled(true); runnable r = new runnable() { @override public void run() { try { url = new url("http://thinktodo.net/locationservicesystem/index.php/location/getlocation?username=1"); conn = (httpurlconnection) url.openconnection(); bufferedreader br = new bufferedreader(new inputstreamreader(conn.getinputstream())); str = br.readline(); jsonobject jsonobject = new jsonobject(str); final double lat = jsonobject.getdouble("lat"); final double lng = jsonobject.getdouble("lng"); latlng = new latlng(lat, lng); marker = new markeroptions().position(new latlng(latlng.latitude, latlng.longitude)).title("you here"); googlemap.addmarker(marker); googlemap.movecamera(cameraupdatefactory.newlatlng(latlng)); googlemap.animatecamera(cameraupdatefactory.zoomto(10)); br.close(); } catch (malformedurlexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } catch (jsonexception e) { e.printstacktrace(); } } }; thread th = new thread(r); th.start(); } } }
}
main_activity.xml:
<?xml version="1.0" encoding="utf-8"?> <fragment xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent" android:name="com.google.android.gms.maps.mapfragment"/>
logcat:
04-23 02:49:51.250: e/socketstream(112): readfully waiting 403440 bytes, got 49152 04-23 02:49:51.254: e/socketstream(112): readfully waiting 354288 bytes, got 49152 04-23 02:49:51.254: e/socketstream(112): readfully waiting 305136 bytes, got 49152 04-23 02:49:51.254: e/socketstream(112): readfully waiting 255984 bytes, got 70084 04-23 02:49:51.254: e/socketstream(112): readfully waiting 185900 bytes, got 49152 04-23 02:49:51.254: e/socketstream(112): readfully waiting 136748 bytes, got 49152 04-23 02:49:51.254: e/socketstream(112): readfully waiting 87596 bytes, got 49152 04-23 02:49:51.298: e/trace(1692): error opening trace file: no such file or directory (2) 04-23 02:49:51.858: e/androidruntime(1692): fatal exception: thread-180 04-23 02:49:51.858: e/androidruntime(1692): java.lang.illegalstateexception: not on main thread 04-23 02:49:51.858: e/androidruntime(1692): @ maps.k.o.b(unknown source) 04-23 02:49:51.858: e/androidruntime(1692): @ maps.i.g.b(unknown source) 04-23 02:49:51.858: e/androidruntime(1692): @ maps.e.al.a(unknown source) 04-23 02:49:51.858: e/androidruntime(1692): @ etl.ontransact(sourcefile:167) 04-23 02:49:51.858: e/androidruntime(1692): @ android.os.binder.transact(binder.java:326) 04-23 02:49:51.858: e/androidruntime(1692): @ com.google.android.gms.maps.internal.igooglemapdelegate$a$a.addmarker(unknown source) 04-23 02:49:51.858: e/androidruntime(1692): @ com.google.android.gms.maps.googlemap.addmarker(unknown source) 04-23 02:49:51.858: e/androidruntime(1692): @ tri.mylocapp.mainactivity$1.run(mainactivity.java:88) 04-23 02:49:51.858: e/androidruntime(1692): @ java.lang.thread.run(thread.java:856) 04-23 02:49:51.930: e/socketstream(112): readfully waiting 403440 bytes, got 49152 04-23 02:49:51.930: e/socketstream(112): readfully waiting 354288 bytes, got 49152 04-23 02:49:51.930: e/socketstream(112): readfully waiting 305136 bytes, got 49152 04-23 02:49:51.930: e/socketstream(112): readfully waiting 255984 bytes, got 49152 04-23 02:49:51.930: e/socketstream(112): readfully waiting 206832 bytes, got 16384 04-23 02:49:51.930: e/socketstream(112): readfully waiting 190448 bytes, got 97988 04-23 02:49:51.934: e/socketstream(112): readfully waiting 92460 bytes, got 49152 04-23 02:49:51.934: e/socketstream(112): readfully waiting 43308 bytes, got 32768
the problem modifying map on different thread should modified on ui thread/main thread..
latlng = new latlng(lat, lng); marker = new markeroptions().position(new latlng(latlng.latitude, latlng.longitude)).title("you here"); googlemap.addmarker(marker); googlemap.movecamera(cameraupdatefactory.newlatlng(latlng)); googlemap.animatecamera(cameraupdatefactory.zoomto(10));
that problem came from...
solution:
use asynctask server connection , parsing on doinbackground , when successfull modifying of map on postexecute
Comments
Post a Comment