php - E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1 java.lang.RuntimeException: An error occured while executing doInBackground() -
i beginner in programming , want create app display markers on map using coordinates database( hosted 000webhost), using php , json.the map working, there no markers. please me because cannot figure out.
this error message get:
e/androidruntime: fatal exception: asynctask #1 process: com.kid.compaq.licenta, pid: 24949 java.lang.runtimeexception: error occured while executing doinbackground() @ android.os.asynctask$3.done(asynctask.java:304) @ java.util.concurrent.futuretask.finishcompletion(futuretask.java:355) @ java.util.concurrent.futuretask.setexception(futuretask.java:222) @ java.util.concurrent.futuretask.run(futuretask.java:242) @ android.os.asynctask$serialexecutor$1.run(asynctask.java:231) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1112) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:587) @ java.lang.thread.run(thread.java:818) caused by: java.lang.illegalstateexception: not on main thread @ maps.f.g.b(unknown source) @ maps.z.d.a(unknown source) @ maps.ag.t.a(unknown source) @ uz.ontransact(:com.google.android.gms.dynamitemodulesb:167) @ android.os.binder.transact(binder.java:385) @ com.google.android.gms.maps.internal.igooglemapdelegate$zza$zza.addmarker(unknown source) @ com.google.android.gms.maps.googlemap.addmarker(unknown source) @ com.kid.compaq.licenta.mapsactivity$markertask.doinbackground(mapsactivity.java:106) @ com.kid.compaq.licenta.mapsactivity$markertask.doinbackground(mapsactivity.java:62) @ android.os.asynctask$2.call(asynctask.java:292) @ java.util.concurrent.futuretask.run(futuretask.java:237) @ android.os.asynctask$serialexecutor$1.run(asynctask.java:231) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1112) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:587) @ java.lang.thread.run(thread.java:818) 05-28 19:22:28.324 731-919/? e/inputdispatcher: channel 'd1579c5 com.kid.compaq.licenta/com.kid.compaq.licenta.mapsactivity (server)' ~ channel unrecoverably broken , disposed! 05-28 19:23:01.289 731-1342/? e/sensors: handletodriver handle(0) 05-28 19:23:01.290 731-1342/? e/sensors: handletodriver handle(0) 05-28 19:23:01.290 731-1342/? e/sensors: new setdelay handle(0),ns(20000000)m, error(0), index(2) 05-28 19:23:01.299 731-939/? e/wifistatemachine: disconnectedstate (when=0 what=131143 arg1=10009 arg2=682) cmd_start_scan 10009 682 ic=0 proc(ms):0 dur:692 cnt=2252 rssi=-64 f=-1 sc=0 link=-1 tx=0,0, 0,0, 0,0 rx=0,0 fiv=20000 [on:0 tx:0 rx:0 period:121028] screen [on:0 period:-131266637] 05-28 19:23:01.299 731-939/? e/wifistatemachine: connectmodestate (when=-1ms what=131143 arg1=10009 arg2=682) cmd_start_scan 10009 682 ic=0 proc(ms):1 dur:692 cnt=2252 rssi=-64 f=-1 sc=0 link=-1 tx=0,0, 0,0, 0,0 rx=0,0 fiv=20000 [on:0 tx:0 rx:0 period:0] screen [on:0 period:-131266637] 05-28 19:23:01.300 731-939/? e/wifistatemachine: driverstartedstate (when=-2ms what=131143 arg1=10009 arg2=682) cmd_start_scan 10009 682 ic=0 proc(ms):2 dur:692 cnt=2252 rssi=-64 f=-1 sc=0 link=-1 tx=0,0, 0,0, 0,0 rx=0,0 fiv=20000 [on:0 tx:0 rx:0 period:1] screen [on:0 period:-131266636] 05-28 19:23:01.300 731-939/? e/wifistatemachine: stopreconnectwifi wfd=false stopscan=false mdontreconnectandscan=false 05-28 19:23:01.302 731-939/? e/wifistatemachine: [1.464.452.581.302 ms] notescanstartworksource{10009} uid 10009 05-28 19:23:01.304 731-10206/? e/wifimonitor: handleevent unknown: 15 ctrl-event-scan-started 05-28 19:23:01.322 1716-1792/? e/celllocation: create gsmcelllocation 05-28 19:23:01.358 1716-1792/? e/celllocation: create gsmcelllocation 05-28 19:23:01.415 1716-25019/? e/mplugin: unsupported class: com.mediatek.common.telephony.ionlyownersimsupport
mapsactivity:
import android.os.asynctask; import android.os.bundle; import android.support.v4.app.fragmentactivity; import android.util.log; import com.google.android.gms.maps.googlemap; import com.google.android.gms.maps.onmapreadycallback; import com.google.android.gms.maps.supportmapfragment; import com.google.android.gms.maps.model.bitmapdescriptorfactory; import com.google.android.gms.maps.model.latlng; import com.google.android.gms.maps.model.markeroptions; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import java.io.bufferedreader; import java.io.inputstream; import java.io.inputstreamreader; import java.net.httpurlconnection; import java.net.url; public class mapsactivity extends fragmentactivity implements onmapreadycallback { httpurlconnection urlconnection= null; private googlemap mmap; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_maps); // obtain supportmapfragment , notified when map ready used. supportmapfragment mapfragment = (supportmapfragment) getsupportfragmentmanager() .findfragmentbyid(r.id.map); mapfragment.getmapasync(this); new markertask().execute(); } /** * manipulates map once available. * callback triggered when map ready used. * can add markers or lines, add listeners or move camera. in case, * add marker near sydney, australia. * if google play services not installed on device, user prompted install * inside supportmapfragment. method triggered once user has * installed google play services , returned app. */ @override public void onmapready(googlemap googlemap) { mmap = googlemap; mmap.getuisettings().iscompassenabled(); mmap.getuisettings().setzoomcontrolsenabled(true); mmap.getuisettings().setscrollgesturesenabled(true); mmap.getuisettings().setmaptoolbarenabled(true); } class markertask extends asynctask<void, string, void> { @override protected void onpreexecute() { super.onpreexecute(); } @override protected void doinbackground(void... params) { inputstream = null; string result=""; try { url url = new url("http://catcat.netne.net/get_mark.php"); urlconnection = (httpurlconnection) url.openconnection(); urlconnection.connect(); = urlconnection.getinputstream(); //2 sec, tel } catch (exception e) { log.e("url connection", e.tostring()); } try{ bufferedreader reader = new bufferedreader(new inputstreamreader(is,"utf-8")); string line= null; while( (line= reader.readline())!=null){ result +=line; } is.close(); } catch (exception e){ e.printstacktrace(); } try{ jsonarray jsonarray = new jsonarray(result); (int = 0; < jsonarray.length(); i++) { jsonobject jsonobj = jsonarray.getjsonobject(i); latlng latlng = new latlng(jsonobj.getjsonarray("latlng").getdouble(0), jsonobj.getjsonarray("latlng").getdouble(1)); // create marker each station in json data. mmap.addmarker(new markeroptions() .icon(bitmapdescriptorfactory.defaultmarker(bitmapdescriptorfactory.hue_blue)) .title(jsonobj.getstring("nume")) .position(latlng)); } } catch (jsonexception e) { e.printstacktrace(); } return null; } // executed after complete execution of doinbackground() method @override protected void onpostexecute (void result){ } } }
php file:
<?php @$dbhandle = mysql_connect($host,$user,$pass)or die("unable connect mysql"); $selected = mysql_select_db($db,$dbhandle)or die("could not select licenta"); $sql="select id_st, nume, concat_ws(\",\",lat, lng) latlng statii"; $result = mysql_query($sql); $response= array(); while($row= mysql_fetch_assoc($result)){ $response[]=array( 'nume' => $row['nume'], 'latlng' => explode(',', $row['latlng']), 'id_st' => $row['id_st'] ); } //$bd_json = json_encode($response); echo json_encode($response); exit(); ?>
json response:
[{ "nume": "merge in sfarsit", "latlng": ["45.659721", "25.606859"], "id_st": "5" }, { "nume": "memo cantina", "latlng": ["45.655075", "25.581560"], "id_st": "6" }]
one issue see in code below adding marker google map (on ui thread) background thread:
// create marker each station in json data. mmap.addmarker(new markeroptions() .icon(bitmapdescriptorfactory.defaultmarker(bitmapdescriptorfactory.hue_blue)) .title(jsonobj.getstring("nume")) .position(latlng));
either create entire set of markers built in loop first, maybe in list, , pass list onpostexecute @ end add them map there, or post each marker using onprogressupdate method like:
// create marker each station in json data. mmarker = new markeroptions().icon(bitmapdescriptorfactory.defaultmarker(bitmapdescriptorfactory.hue_blue)) .title(jsonobj.getstring("nume")) .position(latlng); onprogressupdate(mmarker);
make sure add:
markeroptions mmarker;
below line:
private googlemap mmap;
and override onprogressupdate method:
protected void onprogressupdate(markeroptions marker) { mmap.addmarker(marker); }
don't forget change
class markertask extends asynctask<void, string, void> {
to
class markertask extends asynctask<void, markeroptions, void> {
while might past issue, should review asynctask class , familiar it.
good luck!
Comments
Post a Comment