You are getting the null pointer b/c getLastKnownLocation has nothing to
give... In this case you would need to call up the GPS listener... When it
gets the current fix it will pop off its subroutine.. Inside there you can
get the lat/lon do what you need to do with your button click then kill the
GPS listener..

-----Original Message-----
From: android-developers@googlegroups.com
[mailto:android-developers@googlegroups.com] On Behalf Of Mark Murphy
Sent: Thursday, May 05, 2011 3:29 PM
To: android-developers@googlegroups.com
Subject: Re: [android-developers] Re: Android app crashes when invoking
getSystemService(LOCATION_SERVICE)

requestLocationUpdates() is asynchronous. It may take seconds or more for
the location to be determined, depending on the location provider being used
and the user's circumstances (e.g., is the user getting a GPS signal?). You
cannot simply call getLastKnownLocation() immediately after calling
requestLocationUpdates() and expect to get any value.

Your whole premise (press a button, get a location) will only work if
either:

1. You have location updates started early (e.g., onResume()), so by the
time the user presses the button, you probably have a fix, or

2. Pressing the button triggers the request for the location, and it simply
does not have an immediate effect, until the fix arrives

On Thu, May 5, 2011 at 3:23 PM, Prayag Pathak <prayag.d.pat...@gmail.com>
wrote:
> Hi Mark,
>
> Thanks for the answer. But still its not working. I included the code 
> segment that updates the code. But still not working.
> Following is the code :
>
>        LocationManager lm = (LocationManager) 
> getSystemService(Context.LOCATION_SERVICE);
>        LocationListener ll = new mylocationlistener();
>        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, 
> ll);
>        Location l =
> lm.getLastKnownLocation(lm.getProvider(LOCATION_SERVICE).getName());
>        Log.w("LocationTracker", "The value of Location returned by
> getLastKnownLocation() is : " + l.toString());
>        Log.d("Current Location", l.getLatitude() + "");
>        Log.d("Current Location",l.getLongitude() + "");
>        Toast.makeText(this, l.getLatitude() + "" + 
> l.getLongitude(),Toast.LENGTH_LONG).show();
>
> Regards,
>
> Prayag
>
> On May 5, 2:20 pm, Prayag Pathak <prayag.d.pat...@gmail.com> wrote:
>> Hi guys,
>>
>> I am trying to get the latitude and longitude of the current location.
>> Let me clarify, I dont want to implement a listener that will create 
>> an event when the location will be changed. I am trying to create an 
>> app that will record the current location on a button click event.
>> Following is my code :
>>
>> hdLocMgr = (LocationManager)
>> getSystemService(Context.LOCATION_SERVICE);
>> Log.w("LocationTracker", "The Location Manager is :
>> "+hdLocMgr.toString());
>> hdLocProvider = hdLocMgr.getBestProvider(hdCrit, true); location = 
>> hdLocMgr.getLastKnownLocation(hdLocProvider);
>> dlat = location.getLatitude();
>> dlon = location.getLongitude();
>> Log.w("LocationTracker", "The value of Latitude is :
>> "+dlat.toString());
>>
>> The code gives a RunTimeException (caused by NullPointerException as 
>> stated in it).
>>
>> Kindly help me out in that.
>>
>> Regards,
>>
>> Prayag
>
> --
> You received this message because you are subscribed to the Google 
> Groups "Android Developers" group.
> To post to this group, send email to 
> android-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
>



--
Mark Murphy (a Commons Guy)
http://commonsware.com | http://github.com/commonsguy
http://commonsware.com/blog | http://twitter.com/commonsguy

_Android Programming Tutorials_ Version 3.4 Available!

--
You received this message because you are subscribed to the Google Groups
"Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to