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

The Archive Base Latest Questions

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

Hi dudes I am new to android I am getting null while creating location

  • 0

Hi dudes I am new to android I am getting null while creating location object using LocationManager.GPS_PROVIDER.

Here My GPSDemoActivity :

 import android.app.Activity;
 import android.content.Context;
 import android.location.Location;
 import android.location.LocationListener;
 import android.location.LocationManager;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.Toast;

public class GPSDemoActivity extends Activity {

    private static final long MINIMUM_DISTANCE = 1;
    private static final long MINIMUM_TIME = 1000;

    protected LocationManager locationManager;

    // private Button showlocationButton;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // showlocationButton = (Button) findViewById(R.id.btn_showlocation);
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                MINIMUM_DISTANCE, MINIMUM_TIME, new MyLocationListener());
    }

    public void showLocation(View v) {
        Toast.makeText(this, "Clicked", Toast.LENGTH_LONG).show();
        Location location = locationManager
                .getLastKnownLocation(LocationManager.GPS_PROVIDER);//here getting null.

        if (location != null) {
            String message = String.format(
                    "Current Location: \n Longitude: %1$s \n Latitude: %2$s",
                    location.getLongitude(), location.getLatitude());
            Toast.makeText(this, message, Toast.LENGTH_LONG).show();
        }

    }

    private class MyLocationListener implements LocationListener {

        @Override
        public void onLocationChanged(Location location) {
            String message = String.format(
                    "Your Location \n Longitude: %1$s \n Latitude: %2$s",
                    location.getLongitude(), location.getLatitude());
            Toast.makeText(GPSDemoActivity.this, message, Toast.LENGTH_LONG);
        }

        @Override
        public void onProviderDisabled(String provider) {

            Toast.makeText(GPSDemoActivity.this, "Provider disabled by the user. GPS turned off", Toast.LENGTH_LONG).show();

        }

        @Override
        public void onProviderEnabled(String provider) {

            Toast.makeText(GPSDemoActivity.this,
                    "Provider disabled by the user. GPS turned on",
                    Toast.LENGTH_LONG).show();

        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

            Toast.makeText(GPSDemoActivity.this,
                    "Provider status changed", Toast.LENGTH_LONG).show();


        }

    }
}

Here below My xml.
main.xml:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

   <Button
       android:id="@+id/btn_showlocation" 
       android:layout_height="wrap_content"
       android:layout_width="wrap_content"
       android:text="@string/btn_showlocation"
       android:layout_gravity="center"
       android:onClick="showLocation"
       />

</LinearLayout>

here my manifest.xml.
My manifest.xml:

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.gps"
    android:versionCode="1"
    android:versionName="1.0" >

   <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".GPSDemoActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>


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

</manifest>
  • 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-01T17:11:19+00:00Added an answer on June 1, 2026 at 5:11 pm

    locationManager.getLastKnownLocation returns valid value only if you are able to detect location atleast once.

    Are you sure location search using gps is enabled on your device, only then location manager can retreieve locations

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

Sidebar

Related Questions

Hey dudes.i am having this problem while symlinking. I have successfully deployed a ruby
I am getting an odd error when running $dom = new DOMDocument(1.0, utf-8);: Warning
Using c# 3 and .Net Framework 3.5, I have a Person object public Person
I'm new to groovy, and I'm reading the source of a project gretty import
Hai dudes, Cany anyone tell me how to Create a ProgressDialog in Android?
dudes. I hope this question is not a repost. I'm newbie on Kohana. I'm
I have 2 Lists. var adultList = new List<Dude>(); adultList.Add(new Dude() { ID =
Possible Duplicate: Why won't this generic java code compile? Given the following code: import
I'm new to CouchDB and document-oriented databases in general. I've been playing around with
I am starting a new c++ project and I want to use Boost.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.