android - GPS Co-ordinates app -
i'm trying write simple gps co-ordinate app android keeps on crashing due nullpointerexception. code app looks code other gps co-ordinate apps , i'm struggling find error in code. nullpointerexception seems appear in onlocationchanged method when use following code: longtext.settext("longitude is: " + location.getlongitude()).
the message logcat "onlocationchanged entered" displayed in logcat therefore there location, in other words, (location != null) = true.
see below full code:
public class mainactivity extends activity implements locationlistener { public locationmanager locationmgr; private static final int min_time = 5000; private static final int min_distance = 10; public location currentlocation; textview longtext; textview lattext; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); longtext = (textview) findviewbyid(r.id.latitude); lattext = (textview) findviewbyid(r.id.longitude); locationmgr = (locationmanager) getsystemservice(context.location_service); locationmgr.requestlocationupdates(locationmanager.gps_provider, min_time, min_distance, this); } @override public void onresume() { super.onresume(); try { /* defaulting place */ location hardfix = new location("atl"); hardfix.setlatitude(39.931261); hardfix.setlongitude(-75.051267); hardfix.setaltitude(1); try { location gps = locationmgr.getlastknownlocation(locationmanager.gps_provider); location network = locationmgr.getlastknownlocation(locationmanager.network_provider); if (gps != null) currentlocation = (gps); else if (network != null) currentlocation = (network); else currentlocation = (hardfix); } catch (exception ex2) { currentlocation = (hardfix); } onlocationchanged(currentlocation); } catch (exception ex) { ex.printstacktrace(); } } @override public void onlocationchanged(location location) { // todo auto-generated method stub if (location != null) { log.i("location", "onlocationchanged entered"); longtext.settext("longitude is: " + location.getlongitude()); lattext.settext("latitude is: " + location.getlatitude()); } } public void onpause() { super.onpause(); locationmgr.removeupdates(this); }
my permissions in manifest:
<uses-permission android:name="android.permission.access_fine_location"/> <uses-permission android:name="android.permission.access_coarse_location"/> <uses-permission android:name="android.permission.internet"/>
my logcat:
03-22 14:35:49.695: i/location(13099): onlocationchanged entered 03-22 14:35:49.695: d/androidruntime(13099): shutting down vm 03-22 14:35:49.695: w/dalvikvm(13099): threadid=1: thread exiting uncaught exception (group=0x41df4700) 03-22 14:35:49.695: e/androidruntime(13099): fatal exception: main 03-22 14:35:49.695: e/androidruntime(13099): java.lang.nullpointerexception 03-22 14:35:49.695: e/androidruntime(13099): @ com.example.gpscoord.mainactivity.onlocationchanged(mainactivity.java:75) 03-22 14:35:49.695: e/androidruntime(13099): @ android.location.locationmanager$listenertransport._handlemessage(locationmanager.java:255) 03-22 14:35:49.695: e/androidruntime(13099): @ android.location.locationmanager$listenertransport.access$000(locationmanager.java:184) 03-22 14:35:49.695: e/androidruntime(13099): @ android.location.locationmanager$listenertransport$1.handlemessage(locationmanager.java:200) 03-22 14:35:49.695: e/androidruntime(13099): @ android.os.handler.dispatchmessage(handler.java:99) 03-22 14:35:49.695: e/androidruntime(13099): @ android.os.looper.loop(looper.java:137) 03-22 14:35:49.695: e/androidruntime(13099): @ android.app.activitythread.main(activitythread.java:5419) 03-22 14:35:49.695: e/androidruntime(13099): @ java.lang.reflect.method.invokenative(native method) 03-22 14:35:49.695: e/androidruntime(13099): @ java.lang.reflect.method.invoke(method.java:525) 03-22 14:35:49.695: e/androidruntime(13099): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1209) 03-22 14:35:49.695: e/androidruntime(13099): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1025) 03-22 14:35:49.695: e/androidruntime(13099): @ dalvik.system.nativestart.main(native method) 03-22 14:35:51.430: i/process(13099): sending signal. pid: 13099 sig: 9
it's because have invoked onlocationchanged() in code. don't - it's callback. means driven system event, i.e. getting location fix.
Comments
Post a Comment