android - I am getting the Following error when executing the code -
fatal exception: main java.lang.runtimeexception: unable start activity componentinfo{com.bugs3.munishgupta.cabup/com.bugs3.munishgupta.cabup.wannahier}: java.lang.nullpointerexception @ android.app.activitythread.performlaunchactivity(activitythread.java:1956) @ android.app.activitythread.handlelaunchactivity(activitythread.java:1981) @ android.app.activitythread.access$600(activitythread.java:123) @ android.app.activitythread$h.handlemessage(activitythread.java:1147) @ android.os.handler.dispatchmessage(handler.java:99) @ android.os.looper.loop(looper.java:137) @ android.app.activitythread.main(activitythread.java:4424) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:511) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:825) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:592) @ dalvik.system.nativestart.main(native method) caused by: java.lang.nullpointerexception @ com.bugs3.munishgupta.cabup.wannahier.oncreate(wannahier.java:44) @ android.app.activity.performcreate(activity.java:4465) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1051) @ android.app.activitythread.performlaunchactivity(activitythread.java:1920) ... 11 more
wanna hier.java
package com.bugs3.munishgupta.cabup; import java.util.concurrent.executionexception; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import android.app.activity; import android.content.intent; import android.location.location; import android.os.bundle; import android.util.log; import android.view.menu; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.toast; import com.google.android.gms.maps.googlemap; import com.google.android.gms.maps.googlemap.onmarkerclicklistener; import com.google.android.gms.maps.googlemap.onmylocationchangelistener; import com.google.android.gms.maps.mapfragment; import com.google.android.gms.maps.model.latlng; import com.google.android.gms.maps.model.marker; import com.google.android.gms.maps.model.markeroptions; public class wannahier extends activity { googlemap map; intent info; string mob,lat,lon,title=null; bundle b; button btvcab; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.wanna_hier); info = getintent(); b = info.getextras(); mob = b.getstring("mob"); btvcab=(button) findviewbyid(r.id.btvcab); map=((mapfragment) getfragmentmanager().findfragmentbyid(r.id.map)).getmap(); map.setmylocationenabled(true); lat=double.tostring(map.getmylocation().getlatitude()); lon=double.tostring(map.getmylocation().getlongitude()); map.setonmarkerclicklistener(new onmarkerclicklistener() { @override public boolean onmarkerclick(marker marker) { // todo auto-generated method stub try { title=marker.gettitle().tostring(); request(title,lat,lon,mob); } catch(exception e) { toast.maketext(getapplicationcontext(), "please turn on location", toast.length_long).show(); } return false; } }); map.setonmylocationchangelistener(new onmylocationchangelistener() { @override public void onmylocationchange(location location) { // todo auto-generated method stub jsongetrequest jrequest = jsongetrequest .getsingleinstance(wannahier.this); try { string url = (tags.url + "op=update&query=update requestpick set carno='" +title+ "',latitude='" + lat + "', longitude='" + lon +"'where cust_mob='"+ mob +"'").replaceall( " ", "%20"); jsonobject json = jrequest.execute(url).get(); if (json.getint("success")==1) { log.d("requested successfully!", json.tostring()); } else { log.d("request failed!", json.getstring("message")); } } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (executionexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } } }); btvcab.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub jsongetrequest jrequest = jsongetrequest .getsingleinstance(wannahier.this); try { string url = (tags.url + "op=select&query=select * driverloc") .replaceall(" ", "%20"); jsonobject json = jrequest.execute(url).get(); if (json.getint("success")==1) { log.d("requested successfully!", json.tostring()); jsonarray js=json.getjsonarray("entities"); try{ for(int co=js.length(), i=0; i<co;i++){ json=js.getjsonobject(i); title=json.getstring("carno"); lat=json.getstring("latitude"); lon=json.getstring("longitude"); map.addmarker(new markeroptions() .position(new latlng(double.parsedouble(lat),double.parsedouble(lon))) .title(title)); } } catch(jsonexception e) { e.printstacktrace(); } } else { log.d("request failed!", json.getstring("message")); } } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (executionexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } } }); } @override protected void onstart() { // todo auto-generated method stub super.onstart(); toast.maketext(getapplicationcontext(), "please turn on location , click on view cabs", toast.length_long).show(); } public void request(string title, string lat, string lon, string mob ) { jsongetrequest jrequest = jsongetrequest .getsingleinstance(wannahier.this); try { string url = (tags.url + "op=insert&query=insert requestpick values ('" +title+ "','" + lat + "','" + lon +"','"+ mob +"')").replaceall( " ", "%20"); jsonobject json = jrequest.execute(url).get(); if (json.getint("success")==1) { log.d("requested successfully!", json.tostring()); } else { log.d("request failed!", json.getstring("message")); } } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (executionexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } } @override protected void onpause() { // todo auto-generated method stub super.onpause(); jsongetrequest jrequest = jsongetrequest .getsingleinstance(wannahier.this); try { string url = (tags.url + "op=delete&query=delete requestpick cust_mob='" + mob +"'").replaceall(" ", "%20"); jsonobject json = jrequest.execute(url).get(); if (json.getint("success")==1) { log.d("requested successfully!", json.tostring()); } else { log.d("request failed!", json.getstring("message")); } } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (executionexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } finish(); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.wanna_hier, menu); return true; } }
profile.java
package com.bugs3.munishgupta.cabup; import com.bugs3.munishgupta.cabup.r; import android.app.activity; import android.content.intent; import android.os.bundle; import android.view.menu; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.toast; public class profile extends activity { button obhier, obshare, obsettings; string mob,carno,table; intent info; bundle b; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.profile); obhier=(button) findviewbyid(r.id.btnhier); obshare=(button) findviewbyid(r.id.btnshare); obsettings=(button) findviewbyid(r.id.btnsetting); info = getintent(); b = info.getextras(); table=b.getstring("table"); if((carno=b.getstring("carno"))!=null ) { toast.maketext(getapplicationcontext(), carno, toast.length_short).show(); obhier.setvisibility(view.invisible); } else { obshare.setvisibility(view.invisible); } obshare.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub intent wshare=new intent(profile.this,wannashare.class); mob = b.getstring("mob"); wshare.putextra("mob", mob); wshare.putextra("carno", carno); wshare.putextra("table", table); startactivity(wshare); } }); obhier.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub intent whier=new intent(profile.this,wannahier.class); mob = b.getstring("mob"); whier.putextra("mob", mob); whier.putextra("table", table); startactivity(whier); } }); obsettings.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub intent isetting = new intent(profile.this,changesetting.class); if(b.getstring("mob")!=null) { mob = b.get("mob").tostring(); isetting.putextra("mob", mob); isetting.putextra("table", table); } startactivity(isetting); } }); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.profile, menu); return true; } }
the answer @ wannahier: line 44
suspect
lat=double.tostring(map.getmylocation().getlatitude());
because getmylocation()
can return null
if there no location available have check this.
for example:
location location = map.getmylocation(); if(location != null){ lat=double.tostring(location.getlatitude()); lon=double.tostring(location.getlongitude()); }
Comments
Post a Comment