Reputation: 609
I'm preparing an application that on the click of a button retrieves it's current GPS co-ordinates.
When I placed all my coding in the main activity class, the application executed perfectly and the desired results were displayed.
However, I tried splitting the classes and placing the GPS connection methods in a separate java class and call them from the main activity class.
I managed to analyze the code and figured out that this part is causing the problem(giving a force close error while testing) :
mgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 100,mlocListener);
Can anybody kindly explain why I'm not managing to use the location services in the java class.
01-08 02:21:30.589: I/ApplicationPackageManager(17978): cscCountry is not German : XEO
01-08 02:21:51.499: D/AndroidRuntime(17978): Shutting down VM
01-08 02:21:51.499: W/dalvikvm(17978): threadid=1: thread exiting with uncaught exception (group=0x40018578)
01-08 02:21:51.509: E/AndroidRuntime(17978): FATAL EXCEPTION: main
01-08 02:21:51.509: E/AndroidRuntime(17978): java.lang.NullPointerException
01-08 02:21:51.509: E/AndroidRuntime(17978): at stefan.thesistest.Gpsconnection.connect(Gpsconnection.java:25)
01-08 02:21:51.509: E/AndroidRuntime(17978): at stefan.thesistest.ThesistestActivity$1.onClick(ThesistestActivity.java:41)
01-08 02:21:51.509: E/AndroidRuntime(17978): at android.view.View.performClick(View.java:2485)
01-08 02:21:51.509: E/AndroidRuntime(17978): at android.view.View$PerformClick.run(View.java:9080)
01-08 02:21:51.509: E/AndroidRuntime(17978): at android.os.Handler.handleCallback(Handler.java:587)
01-08 02:21:51.509: E/AndroidRuntime(17978): at android.os.Handler.dispatchMessage(Handler.java:92)
01-08 02:21:51.509: E/AndroidRuntime(17978): at android.os.Looper.loop(Looper.java:123)
01-08 02:21:51.509: E/AndroidRuntime(17978): at android.app.ActivityThread.main(ActivityThread.java:3687)
01-08 02:21:51.509: E/AndroidRuntime(17978): at java.lang.reflect.Method.invokeNative(Native Method)
01-08 02:21:51.509: E/AndroidRuntime(17978): at java.lang.reflect.Method.invoke(Method.java:507)
01-08 02:21:51.509: E/AndroidRuntime(17978): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
01-08 02:21:51.509: E/AndroidRuntime(17978): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
01-08 02:21:51.509: E/AndroidRuntime(17978): at dalvik.system.NativeStart.main(Native Method)
Main Activity class on click method:
output = (TextView) findViewById(R.id.textView1);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Gpsconnection.connect();
int test4 = Global.flag;
double test1 = Global.lat1;
double test2 = Global.lat2;
String tot = new Double(test1).toString();
String tot2 = new Double(test2).toString();
if(test4 == 2 ){
output.setText("GPS IS NOT ON");
}
if(test4 ==3){
output.setText("STILL WAITING CONECTION");
}
if(test4 ==4){
output.setText("Latitude:- " + tot
+ '\n' + "Longitude:- " + tot2
+ '\n');
}
};
});
Java class( gps retrieval methods):
public static void connect(){
LocationManager mgr;
mgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new LocationManagerHelper();
mgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 100,mlocListener);
if (mgr.isProviderEnabled(LocationManager.GPS_PROVIDER)) {// if gps is on
Global.flag=1;
Global.lat1 = LocationManagerHelper.getLatitude();
Global.lat2 = LocationManagerHelper.getLongitude();
if(Global.lat1 == 0.0 && Global.lat2 == 0.0){
Global.flag=3;
}
else{Global.flag=4;}
} else {
Global.flag=2;//gps is off
}
}
public static class LocationManagerHelper implements LocationListener {
private static double latitude;
private static double longitude;
@Override
public void onLocationChanged(Location loc) {
latitude = loc.getLatitude();
longitude = loc.getLongitude();
}
@Override
public void onProviderDisabled(String provider) { }
@Override
public void onProviderEnabled(String provider) { }
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
public static double getLatitude() {
return latitude;
}
public static double getLongitude() {
return longitude;
}
}
Upvotes: 0
Views: 1406
Reputation: 10285
you have a NullPointerException
. Check for uninitialised variables
UPDATE :
Can you please check using a debugger if mgr
is not null at the line mgr.requestLocationUpdates
?
Upvotes: 1
Reputation: 13474
I think the problem is that when you call:
Global.lat1 = LocationManagerHelper.getLatitude();
Global.lat2 = LocationManagerHelper.getLongitude();
the gps have not fixed the location yet and your latitude and longitude are not yet defined. The point of using listeners is that time is needed to have a location fix and those methods will not work when called immediately after requestUpdates(). I am surprised that they did when your classes were merged, could that be that your gps was already on and connected to satellites? In that exceptional case it would work.
Instead, it is in your onLocationChange(Location change) that an action must be triggered in your activity, and it can be achieved with a call back.
This excellent post provides a good example and a class you can use.
Upvotes: 1