exception - java.lang.nullpointerexception attempt to invoke method 'int java.util.List.size()' -
main activity
trying add google map please u can
import java.util.arraylist; import java.util.hashmap; import java.util.list; import org.json.jsonobject; import android.app.actionbar; import android.content.intent; import android.graphics.color; import android.os.asynctask; import android.os.bundle; import android.support.v4.app.fragmentactivity; import android.util.log; import android.view.menu; import android.view.menuitem; import com.google.android.gms.maps.cameraupdatefactory; import com.google.android.gms.maps.googlemap; import com.google.android.gms.maps.supportmapfragment; import com.google.android.gms.maps.model.latlng; import com.google.android.gms.maps.model.markeroptions; import com.google.android.gms.maps.model.polylineoptions; import com.parse.parseanalytics; import com.parse.parseuser; public class onscreenactivity extends fragmentactivity { private static final latlng sydney = new latlng(30.8894669, 75.8246729); private static final latlng connct = new latlng(30.8914863, 75.874398); googlemap googlemap; final string tag1 = "pathgooglemapactivity"; public static final string tag = onscreenactivity.class.getsimplename(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_map); supportmapfragment fm = (supportmapfragment) getsupportfragmentmanager() .findfragmentbyid(r.id.fragment1); googlemap = fm.getmap(); parseanalytics.trackappopened(getintent()); parseuser currentuser = parseuser.getcurrentuser(); if (currentuser == null) { navigatetologin(); } else { log.i(tag, currentuser.getusername()); } actionbar actionbar = getactionbar(); actionbar.show(); markeroptions options = new markeroptions(); options.position(sydney); options.position(connct); googlemap.addmarker(options); string url = getmapsapidirectionsurl(); readtask downloadtask = new readtask(); downloadtask.execute(url); googlemap.movecamera(cameraupdatefactory.newlatlngzoom(connct, 16)); addmarkers(); } private void addmarkers() { // todo auto-generated method stub if (googlemap != null) { googlemap.addmarker(new markeroptions().position(sydney).title( "first point")); googlemap.addmarker(new markeroptions().position(connct).title( "second point")); } } private string getmapsapidirectionsurl() { string waypoints = "waypoints=optimize:true|" + sydney.latitude + "," + sydney.longitude + "|" + "|" + connct.latitude + "," + connct.longitude; string sensor = "sensor=false"; string params = waypoints + "&" + sensor; string output = "json"; string url = "https://maps.googleapis.com/maps/api/directions/" + output + "?" + params; return url; } private class readtask extends asynctask<string, void, string> { @override protected string doinbackground(string... url) { string data = ""; try { httpconnection http = new httpconnection(); data = http.readurl(url[0]); } catch (exception e) { log.d("background task", e.tostring()); } return data; } @override protected void onpostexecute(string result) { super.onpostexecute(result); new parsertask().execute(result); } } private class parsertask extends asynctask<string, integer, list<list<hashmap<string, string>>>> { @override protected list<list<hashmap<string, string>>> doinbackground( string... jsondata) { jsonobject jobject; list<list<hashmap<string, string>>> routes = null; try { jobject = new jsonobject(jsondata[0]); pathjsonparser parser = new pathjsonparser(); routes = parser.parse(jobject); } catch (exception e) { e.printstacktrace(); } return routes; } @override protected void onpostexecute(list<list<hashmap<string, string>>> routes) { arraylist<latlng> points = null; polylineoptions polylineoptions = null; // traversing through routes (int = 0; < routes.size(); i++) { points = new arraylist<latlng>(); polylineoptions = new polylineoptions(); list<hashmap<string, string>> path = routes.get(i); (int j = 0; j < path.size(); j++) { hashmap<string, string> point = path.get(j); double lat = double.parsedouble(point.get("lat")); double lng = double.parsedouble(point.get("lng")); latlng position = new latlng(lat, lng); points.add(position); } polylineoptions.addall(points); polylineoptions.width(2); polylineoptions.color(color.blue); } googlemap.addpolyline(polylineoptions); } } private void navigatetologin() { intent intent = new intent(this, login.class); intent.addflags(intent.flag_activity_new_task); intent.addflags(intent.flag_activity_clear_task); startactivity(intent); } public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.map, menu); return true; } public boolean onoptionsitemselected(menuitem item) { int itemid = item.getitemid(); if (itemid == r.id.action_logout) { parseuser.logout(); navigatetologin(); } if (itemid == r.id.options) { intent intent = new intent(this, useroptions.class); intent.addflags(intent.flag_activity_new_task); intent.addflags(intent.flag_activity_clear_task); startactivity(intent); } return super.onoptionsitemselected(null); } }
it impossible tell why routes
list null in onpostexecute
in parsertask, is.
if intended doinbackground
in parsertask can return null, should @ least check if routes variable null before attempt it. not fix error , might result in app not doing should, mentioned previously, it's impossible tell more without full log.
if (routes == null) return;
suppress error, solve problem if read multiple json files , of them parse.
Comments
Post a Comment