java - Android Location DistanceTo null -
java - Android Location DistanceTo null -
package com.competitivegamingaudio; import android.app.activity; import android.content.context; import android.location.*; import android.os.bundle; import android.util.log; import android.widget.*; import com.google.android.maps.*;; public class findmyfriendsactivity extends activity { private static final string tag = "findmyfriendsactivity"; public textview locationlabel; public textview distancelabel; public mapview mymapview; public location oldlocation; public location currentlocation; @override public void onresume() { super.onresume(); locationmanager locationmanager=(locationmanager) this.getsystemservice(context.location_service); locationmanager.requestlocationupdates(locationmanager.network_provider,0,0,locationlistener); locationmanager.requestlocationupdates(locationmanager.gps_provider,0,0,locationlistener); } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); locationlabel=(textview) findviewbyid(r.id.locationlabel); distancelabel=(textview)findviewbyid(r.id.distancelabel); } locationlistener locationlistener =new locationlistener() { public void onlocationchanged (location location) { currentlocation=new location(location); locationlabel.settext(location.tostring()); oldlocation=new location(location); distancelabel.settext("distance: "+calculatedistance(currentlocation,oldlocation)); } public string calculatedistance(location a, location b) { a.distanceto(b); homecoming ""+a; } public void onstatuschanged(string provider, int status, bundle extras) {} public void onproviderenabled(string provider) { } public void onproviderdisabled(string provider) {} }; }
i'm having problem getting code work properly. want able calculate distance between 2 locations. initial location that's beingness passed onlocationchanged method working (which proves have right conditions), because when locationlabel.settext(location.tostring());
works without getting null pointer exceptions. however, when calculatedistance
method called in line 49, a.distanceto(b);
line fails on line 54. thought why distanceto method keeps failing?
error message:
01-09 20:11:58.085: error/androidruntime(9716): java.lang.nullpointerexception 01-09 20:11:58.085: error/androidruntime(9716): @ com.competitivegamingaudio.findmyfriendsactivity$1.onlocationchanged(findmyfriendsactivity.java:47) 01-09 20:11:58.085: error/androidruntime(9716): @ android.location.locationmanager$listenertransport._handlemessage(locationmanager.java:227) 01-09 20:11:58.085: error/androidruntime(9716): @ android.location.locationmanager$listenertransport.access$000(locationmanager.java:160) 01-09 20:11:58.085: error/androidruntime(9716): @ android.location.locationmanager$listenertransport$1.handlemessage(locationmanager.java:176) 01-09 20:11:58.085: error/androidruntime(9716): @ android.os.handler.dispatchmessage(handler.java:99) 01-09 20:11:58.085: error/androidruntime(9716): @ android.os.looper.loop(looper.java:130) 01-09 20:11:58.085: error/androidruntime(9716): @ android.app.activitythread.main(activitythread.java:3821) 01-09 20:11:58.085: error/androidruntime(9716): @ java.lang.reflect.method.invokenative(native method) 01-09 20:11:58.085: error/androidruntime(9716): @ java.lang.reflect.method.invoke(method.java:507) 01-09 20:11:58.085: error/androidruntime(9716): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:839) 01-09 20:11:58.085: error/androidruntime(9716): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:597) 01-09 20:11:58.085: error/androidruntime(9716): @ dalvik.system.nativestart.main(native method)
main.xml:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <textview android:id="@+id/locationlabel" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <textview android:id="@+id/distancelabel" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </linearlayout>
ok, there lot of things i'd alter code here's not place. effort prepare problem seek this...
// bundle , imports here public class findmyfriendsactivity extends activity { // tag, textviews, mapview, locations before add together // locationmanager , locationlistener here below... locationmanager locationmanager = null; locationlistener locationlistener = null; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); locationlabel = (textview)findviewbyid(r.id.locationlabel); distancelabel = (textview)findviewbyid(r.id.distancelabel); locationmanager=(locationmanager) this.getsystemservice(context.location_service); locationlistener = new locationlistener() { // set locationlistener methods here } } @override public void onresume() { super.onresume(); locationmanager.requestlocationupdates(locationmanager.network_provider, 0, 0, locationlistener); locationmanager.requestlocationupdates(locationmanager.gps_provider, 0, 0, locationlistener); } }
java android gps location
Comments
Post a Comment