Hi..All,

                    I need to disply  current Position latitude and
logintude .. I tried but it will show Null pointer exception.. here
with i attached my code... please help me to solve this problem..
I run this application on android emulator

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

public class Home extends Activity
{
        private Button btn1;
        private Button btn2;
        private Button btn3;
        private LocationListener myLocationListener;
        private LocationManager myLocationManager;
        public String latitude;
        public String longitude;

        /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);

        myLocationManager =
(LocationManager)getSystemService(Context.LOCATION_SERVICE);
        myLocationListener = new MyLocationListener();
 
myLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
1, 1, myLocationListener);

        
latitude=String.valueOf(myLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLatitude());
                longitude =
String.valueOf(myLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLongitude());



                btn1= (Button)findViewById(R.id.btn1);
                btn2= (Button)findViewById(R.id.btn2);
                btn3= (Button)findViewById(R.id.btn3);

                redbtn.setOnClickListener(new OnClickListener()
                {

                        @Override
                        public void onClick(View v)
                        {

                                Toast.makeText(getBaseContext(), "Latitude :" +
latitude.toString(), Toast.LENGTH_SHORT).show();
                        }
                });

                greenBtn.setOnClickListener(new OnClickListener()
                {

                        @Override
                        public void onClick(View v) {
                                // TODO Auto-generated method stub
                                Toast.makeText(getBaseContext(), "Longitude:" +
longitude.toString(), Toast.LENGTH_SHORT).show();
                        }
                });

                cancelBtn.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v)
                        {
                                // TODO Auto-generated method stub
                                Toast.makeText(Home.this, "NO gps", 
Toast.LENGTH_SHORT).show();

                        }
                });


        }

         public class MyLocationListener implements LocationListener
         {
                        public void onLocationChanged(Location argLocation)
                        {
                                if(argLocation != null)
                                {
                                        latitude = 
String.valueOf(argLocation.getLatitude());
                            longitude = 
String.valueOf(argLocation.getLongitude());
                        }
                        }
                    public void onProviderDisabled(String provider)
                    {
                    }
                public void onProviderEnabled(String provider)
                {
                }

                    public void onStatusChanged(String provider, int status, 
Bundle
arg2)
                    {
                    }

        };

}


Thanks & Warm Regards,
gold

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

Reply via email to