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 4106964
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:26:39+00:00 2026-05-20T21:26:39+00:00

as the title i have a question about the gps api follow is my

  • 0

as the title i have a question about the gps api
follow is my code

 LocationManager loctionManager; 
                 String contextService=Context.LOCATION_SERVICE;
                 loctionManager=(LocationManager) getSystemService(contextService); 
                 Criteria criteria = new Criteria(); 
                    criteria.setAccuracy(Criteria.ACCURACY_FINE);
                    criteria.setAltitudeRequired(false);
                    criteria.setBearingRequired(false); 
                    criteria.setCostAllowed(true); 
                    criteria.setPowerRequirement(Criteria.POWER_LOW);
                    String provider = loctionManager.getBestProvider(criteria, true); 
                    Location location = loctionManager.getLastKnownLocation(provider);


                     double a=location.getLatitude();
                    double b=location.getLongitude();
Log.d(""+a,""+b);

then the error is java.lang.NullPointerException
anyone can help me?thx

  • 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-05-20T21:26:39+00:00Added an answer on May 20, 2026 at 9:26 pm

    getLastKnownLocation() can return null you need to ensure it does not.

    In order to get a Location you need at first to set permission in your Manifest.

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    

    Then in your code you need to listen for location avalability. You do that by registering a LocationListener. The activity will display, and when Android will have a location for you it will call you back.

    private final LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
        latitude = location.getLatitude();
        longitude = location.getLongitude();
        //Do what you want with your coordinates
    }
    loctionManager.requestLocationUpdates(LocationManager.GPS, 100, 1, locationListener);
    

    If you are using the emulator and not a real device you need to force a location, you can use DDMS inside Eclipse to send a fake location:

    Send a fake location to an Android Emulator

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

Sidebar

Related Questions

Sorry about the extremely vague question title (any suggestions for improvements welcome) I have
Sorry about the wording for my question title. I have a basic HTML anchor
the title might be too generic, but I have a very specific question about
I have question about parsing a specific json string. I didn't find anything which
Hi I have a question about Html.ActionLink. Imagine: @Html.ActionLink(item.title, Singlet, new { id =
i have a question about gmail gdata api and his implementation on c#. I
I have a question about the objective-C. I use the following code to add
I have previously asked a question about how can I write a code block
I have a question regarding building GUI applications. I feel like the title of
Some Windows native applications have a question mark icon on the title bar. It's

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.