I have to write an application which does several things, including recording the GPS location of the user when a correct username and password is entered. I only want the latitude and longitude at that exact moment, no recurring checks or anything of that nature. I think I found something similar to what I was looking for:
How to call to get a single gps fix when ever i want android?
However, eclipse didn't like the context since I hadn't initialized it yet. So here is that entire section of the code:
Context context = getBaseContext();
LocationManager locMan = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
Log.i(getClass().getSimpleName(),"Location Manager called");
Location loc = locMan.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Log.i(getClass().getSimpleName(),"Last known location received");
Double latitude = loc.getLatitude();
Log.i(getClass().getSimpleName(),"Latitude:"+latitude);
Double longitude = loc.getLongitude();
Log.i(getClass().getSimpleName(),"Longitude:"+longitude);
Toast.makeText(context, latitude+","+longitude, Toast.LENGTH_LONG).show();
And for permissions in the manifest:
<uses-permission android:name="com.foxcatch.code.insecure.LOCATION_FINDER" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
However this crashes when I try to access the latitude and longitude and assign them to doubles:
Double latitude = loc.getLatitude();
Double longitude = loc.getLongitude();
Here's the LogCat Error entry, its a little over my head:
04-28 18:00:39.346: E/AndroidRuntime(32548): FATAL EXCEPTION: main 04-28 18:00:39.346: E/AndroidRuntime(32548): Process: com.rps.personaldata, PID: 32548 04-28 18:00:39.346: E/AndroidRuntime(32548): java.lang.NullPointerException 04-28 18:00:39.346: E/AndroidRuntime(32548): at com.rps.personaldata.MainActivity$1.onClick(MainActivity.java:58) 04-28 18:00:39.346: E/AndroidRuntime(32548): at android.view.View.performClick(View.java:4445) 04-28 18:00:39.346: E/AndroidRuntime(32548): at android.view.View$PerformClick.run(View.java:18429) 04-28 18:00:39.346: E/AndroidRuntime(32548): at android.os.Handler.handleCallback(Handler.java:733) 04-28 18:00:39.346: E/AndroidRuntime(32548): at android.os.Handler.dispatchMessage(Handler.java:95) 04-28 18:00:39.346: E/AndroidRuntime(32548): at android.os.Looper.loop(Looper.java:136) 04-28 18:00:39.346: E/AndroidRuntime(32548): at android.app.ActivityThread.main(ActivityThread.java:5081) 04-28 18:00:39.346: E/AndroidRuntime(32548): at java.lang.reflect.Method.invokeNative(Native Method) 04-28 18:00:39.346: E/AndroidRuntime(32548): at java.lang.reflect.Method.invoke(Method.java:515) 04-28 18:00:39.346: E/AndroidRuntime(32548): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781) 04-28 18:00:39.346: E/AndroidRuntime(32548): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 04-28 18:00:39.346: E/AndroidRuntime(32548): at dalvik.system.NativeStart.main(Native Method)
I'm new to android, and I'm just trying to figure all of this out. Thanks for any help.