How to get Location object in android
<p>Hi dudes I am new to android I am getting null while creating location object using LocationManager.GPS_PROVIDER.</p>

<p>Here My GPSDemoActivity :</p>

<pre><code> 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();


        }

    }
}
</code></pre>

<p>Here below My xml.
main.xml:</p>

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

   &lt;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"
       /&gt;

&lt;/LinearLayout&gt;
</code></pre>

<p>here my manifest.xml.
My manifest.xml:</p>

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

   &lt;uses-sdk android:minSdkVersion="8" /&gt;

    &lt;application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" &gt;
        &lt;activity
            android:name=".GPSDemoActivity"
            android:label="@string/app_name" &gt;
            &lt;intent-filter&gt;
                &lt;action android:name="android.intent.action.MAIN" /&gt;

                &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;
    &lt;/application&gt;


    &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/&gt;
    &lt;uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/&gt;
    &lt;uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/&gt;

&lt;/manifest&gt;
</code></pre>
