Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7710697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:11:27+00:00 2026-06-01T01:11:27+00:00

I’ve got an app that uses MapView and I print out the user’s latitude,

  • 0

I’ve got an app that uses MapView and I print out the user’s latitude, longitude and horizontal accuracy to some labels. This all works fine on my HTC Wildfire, but on my SE Xperia the app crashes whenever I try to touch Location.getLatitude(), Location.getLongitude() or Location.getAccuracy().

I’ve got a hunch that it could be the GPS on the Xperia being so slow the location manager hasn’t obtained its coordinates when I’m polling for lat, long and accuracy – but how can i safeguard against this?

Here’s the snippet:

        mapView.setBuiltInZoomControls(false);
        mc = mapView.getController();
        int maxLat = (int) (34.07687 * 1E6);
        int maxLon = (int) (-118.438239 * 1E6);
        int minLat = (int) (34.06489 * 1E6);
        int minLon = (int) (-118.452358 * 1E6);
        List <Overlay> overlays = mapView.getOverlays();

        MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this, mapView);
        myLocationOverlay.enableMyLocation();
        overlays.add(myLocationOverlay);

        mc.zoomToSpan(Math.abs(maxLat - minLat), Math.abs(maxLon - minLon));

        lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);    
        Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 



        GeoPoint gp = new GeoPoint((int)(location.getLatitude() * 1E6), (int)(location.getLongitude() * 1E6));

LOGCAT OUTPUT:

01-09 13:32:04.086: W/dalvikvm(1794): threadid=1: thread exiting with uncaught exception (group=0x2aac8560)
01-09 13:32:04.086: E/AndroidRuntime(1794): FATAL EXCEPTION: main
01-09 13:32:04.086: E/AndroidRuntime(1794): java.lang.NullPointerException
01-09 13:32:04.086: E/AndroidRuntime(1794):     at no.tibeapp.sno.UlovligeGarnActivity$1.run(UlovligeGarnActivity.java:180)
01-09 13:32:04.086: E/AndroidRuntime(1794):     at android.os.Handler.handleCallback(Handler.java:587)
01-09 13:32:04.086: E/AndroidRuntime(1794):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-09 13:32:04.086: E/AndroidRuntime(1794):     at android.os.Looper.loop(Looper.java:123)
01-09 13:32:04.086: E/AndroidRuntime(1794):     at android.app.ActivityThread.main(ActivityThread.java:3701)
01-09 13:32:04.086: E/AndroidRuntime(1794):     at java.lang.reflect.Method.invokeNative(Native Method)
01-09 13:32:04.086: E/AndroidRuntime(1794):     at java.lang.reflect.Method.invoke(Method.java:507)
01-09 13:32:04.086: E/AndroidRuntime(1794):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862)
01-09 13:32:04.086: E/AndroidRuntime(1794):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
01-09 13:32:04.086: E/AndroidRuntime(1794):     at dalvik.system.NativeStart.main(Native Method)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T01:11:28+00:00Added an answer on June 1, 2026 at 1:11 am

    That’s because LocationManager.getLastKnownLocation() can return null.

    In your code, you requested GPS provider to update your deivce’s location.
    But when device’s GPS is off, that method return null.

    And if any requests hasn’t been processed through LocationManager, LocationManager.getLastKnownLocation() return null value as well.
    Because LocationManager doesn’t have a “Last Known Locations” value.

    (You can check LocationManager’s state with following adb command)

    $adb shell dumpsys location
    

    Therefore, if you want to get success with your code snippet, your device’s GPS is ON and LocationManager has a “Last Known Locations”

    Well, I use LocationManager.getLastKnownLocation() method as well, but slightly different .

    I just check if there is any “Last Known Locations” value with LocationManager.getLastKnownLocation() method. Because that is fastest way to resolve current location value.
    However, If it return null, then request location update through requestLocationUpdates.

    Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
    if (location == null) {
        // request location update!!
        lm.requestLocationUpdates (LocationManager.GPS_PROVIDER, 0, 0, listener);
    }
    

    There is also some technic resolve device’s location, I think that’s gonna be huge answer.
    Instead, I will link this video which is great explanation about location. (and other more tips!!)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.