Your code doesn't seem to assign any values to redbtn and greenbtn.

Then you call methods on these objects - perhaps they are still Null at that
point?

--
Kostya Vasilyev -- http://kmansoft.wordpress.com

29.09.2010 0:34 пользователь "Gold" <thangadura...@gmail.com> написал:

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 android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com<android-developers%2bunsubscr...@googlegroups.com>
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

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

Reply via email to