java - Android Iterate Over List -MapView -
i'm attempting iterate on array list running errors when try create dynamic map marker on mapview. casting values not seem work @ bottom of method when try loop iterate on both latitude , longitude values in allpostmarkers json array. please let me know if need more information me , happy provide more code. i'm still iterating on array list problem.
private jsonarray allpostsmarkers = null; private arraylist<hashmap<string, string>> mpostsmarkerlist; public void updatejsondata() { // instantiate arraylist contain json data. // going use bunch of key-value pairs, referring // json element name, , content, example, // message tag, , "i'm awesome" content.. mpostsmarkerlist = new arraylist<hashmap<string, string>>(); // bro, it's time power j parser jsonparser jparser = new jsonparser(); // feed beast our comments url, , spits // json object. boo-yeah jerome. jsonobject json = jparser.getjsonfromurl(map_posts_url); // when parsing json stuff, should // try catch exceptions: try { // know said check if "posts avail." (success==1) // before tried read individual posts, lied... // mcomments tell how many "posts" or comments // available allpostsmarkers = json.getjsonarray(tag_posts); // looping through posts according json object returned (int = 0; < allpostsmarkers.length(); i++) { jsonobject c = allpostsmarkers.getjsonobject(i); // gets content of each tag string content = c.getstring(tag_message); //string username = c.getstring(tag_username); string longitude = c.getstring(tag_longitude); string latitude = c.getstring(tag_latitude); string category = c.getstring(tag_category); system.out.print(latitude); system.out.print(longitude); system.out.print(content); // creating new hashmap hashmap<string, string> map = new hashmap<string, string>(); map.put(tag_message, content); //map.put(tag_username, username); //map.put(tag_longitude, longitude); //map.put(tag_latitude, latitude); map.put(tag_category, category); // adding hashlist arraylist mpostsmarkerlist.add(map); //causing issues latlng shipborough = new latlng(float.parsefloat(latitude), float.parsefloat(longitude)); mmap.addmarker(new markeroptions().position(shipborough).title("post marker")); }
logcat:
08-17 19:31:12.415: e/androidruntime(5468): fatal exception: asynctask #2 08-17 19:31:12.415: e/androidruntime(5468): process: com.rynovation.kline, pid: 5468 08-17 19:31:12.415: e/androidruntime(5468): java.lang.runtimeexception: error occured while executing doinbackground() 08-17 19:31:12.415: e/androidruntime(5468): @ android.os.asynctask$3.done(asynctask.java:300) 08-17 19:31:12.415: e/androidruntime(5468): @ java.util.concurrent.futuretask.finishcompletion(futuretask.java:355) 08-17 19:31:12.415: e/androidruntime(5468): @ java.util.concurrent.futuretask.setexception(futuretask.java:222) 08-17 19:31:12.415: e/androidruntime(5468): @ java.util.concurrent.futuretask.run(futuretask.java:242) 08-17 19:31:12.415: e/androidruntime(5468): @ android.os.asynctask$serialexecutor$1.run(asynctask.java:231) 08-17 19:31:12.415: e/androidruntime(5468): @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1112) 08-17 19:31:12.415: e/androidruntime(5468): @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:587) 08-17 19:31:12.415: e/androidruntime(5468): @ java.lang.thread.run(thread.java:841) 08-17 19:31:12.415: e/androidruntime(5468): caused by: java.lang.illegalstateexception: not on main thread 08-17 19:31:12.415: e/androidruntime(5468): @ mut.b(unknown source) 08-17 19:31:12.415: e/androidruntime(5468): @ owa.b(unknown source) 08-17 19:31:12.415: e/androidruntime(5468): @ oyf.a(unknown source) 08-17 19:31:12.415: e/androidruntime(5468): @ grl.ontransact(sourcefile:167) 08-17 19:31:12.415: e/androidruntime(5468): @ android.os.binder.transact(binder.java:361) 08-17 19:31:12.415: e/androidruntime(5468): @ com.google.android.gms.maps.internal.igooglemapdelegate$a$a.addmarker(unknown source) 08-17 19:31:12.415: e/androidruntime(5468): @ com.google.android.gms.maps.googlemap.addmarker(unknown source) 08-17 19:31:12.415: e/androidruntime(5468): @ com.rynovation.kline.locationviewcontroller.updatejsondata(locationviewcontroller.java:187) 08-17 19:31:12.415: e/androidruntime(5468): @ com.rynovation.kline.locationviewcontroller$loadmapmarkers.doinbackground(locationviewcontroller.java:224) 08-17 19:31:12.415: e/androidruntime(5468): @ com.rynovation.kline.locationviewcontroller$loadmapmarkers.doinbackground(locationviewcontroller.java:1) 08-17 19:31:12.415: e/androidruntime(5468): @ android.os.asynctask$2.call(asynctask.java:288) 08-17 19:31:12.415: e/androidruntime(5468): @ java.util.concurrent.futuretask.run(futuretask.java:237) 08-17 19:31:12.415: e/androidruntime(5468): ... 4 more
you cannot changes map if not on main (ui) thread. therefor the:
java.lang.illegalstateexception: not on main thread
exception.
either on main thread or create handler
(which run later on main thread):
handler handler = new handler(looper.getmainlooper()); handler.post(new runnable(){ public void run() { // code touches map here } }
in case simple way post each new marker (which inefficient):
handler handler = new handler(looper.getmainlooper()); (int = 0; < allpostsmarkers.length(); i++) { ... final latlng shipborough = new latlng(float.parsefloat(latitude), float.parsefloat(longitude)); handler.post(new runnable() { public void run() { mmap.addmarker(new markeroptions().position(shipborough) .title("post marker")); } }); }
Comments
Post a Comment