1

I wrote a program to show my current location, Address etc in android phone. The API used is Android 2.2. Previously it worked fine in a MTS CDMA phone. It had an API of Android 2.3. It also works fine in emulator. Now. I have bought a Sony Ericsson Xperia Minipro and it also has Android 2.3. But the program will run on it. But the edit texts will not show anything. I use edit texts to display the values. They are not updated by the program at all. What can be the problem? Same program works fine in emulator and it shows location! Please help. I can paste the code below

public class TravelGuide extends Activity {
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES=1;
private static final long MINIMUM_TIME_BETWEEN_UPDATES=1000;
StringBuilder strReturnedAddress;
List<Address> addresses;
protected LocationManager locationManager;
static String videoId;
Double lat;
Double longd;
Address returnedAddress;

private final LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
       //Double latitude = location.getLatitude();
       //Double longitude = location.getLongitude();

    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }
  };
/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MINIMUM_TIME_BETWEEN_UPDATES,
            MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,new MyLocationListener());
    showCurrentLocation();
}
public void showCurrentLocation()
{
    Location location=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    if(location!=null)
    {
        TextView myLatitude = (TextView)findViewById(R.id.mylatitude);
        TextView myLongitude = (TextView)findViewById(R.id.mylongitude);
        TextView myAddress = (TextView)findViewById(R.id.myaddress);

        String message=String.format("currentLocation\n Longitude:%1$s \n Latitude:%2$s",location.getLongitude(),location.getLatitude());
        Toast.makeText(TravelGuide.this,message,Toast.LENGTH_LONG).show();
        myLatitude.setText("Latitude: " + String.valueOf(location.getLatitude()));
            myLongitude.setText("Longitude: " + String.valueOf(location.getLongitude()));
           Geocoder geocoder = new Geocoder(this, Locale.ENGLISH);
          try {
          List<Address> addresses = geocoder.getFromLocation(Double.valueOf(location.getLatitude()), 
                  Double.valueOf(location.getLongitude()), 1);

          if(addresses != null) {
           Address returnedAddress = addresses.get(0);
           StringBuilder strReturnedAddress = new StringBuilder("Address:\n");
           for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
            strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
           }
        videoId = (returnedAddress.getAddressLine(1)).toString();
        //sendAddress(videoId);
           myAddress.setText(strReturnedAddress.toString());
          }
          else{
           myAddress.setText("No Address returned!");
          }
         } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
          //test = "kovalam";
          myAddress.setText("Canont get Address!");


    }

}

}


        public class MyLocationListener implements LocationListener
        {
            //private final Context MyLocationListener = null;

            //double LATITUDE;
            //double LONGITUDE;
            TextView myLatitude;
            TextView myLongitude;
            TextView myAddress;
        public void onLocationChanged(Location location)
        {
            //Context context = null;
            myLatitude = (TextView)findViewById(R.id.mylatitude);
            myLongitude = (TextView)findViewById(R.id.mylongitude);
            myAddress = (TextView)findViewById(R.id.myaddress);
            //String message=String.format("new Location\n Longitude:%1$s \n Latitude:%2$s",
            //location.getLongitude(),location.getLatitude());
            //Toast.makeText(TouristGuideActivity.this,message,Toast.LENGTH_LONG).show();
            myLatitude.setText("Latitude: " + String.valueOf(location.getLatitude()));
            myLongitude.setText("Longitude: " + String.valueOf(location.getLongitude()));
           Geocoder geocoder = new Geocoder(TravelGuide.this, Locale.ENGLISH);
          try {
          List<Address> addresses = geocoder.getFromLocation(Double.valueOf(location.getLatitude()), 
                  Double.valueOf(location.getLongitude()), 1);

          if(addresses != null) {
           Address returnedAddress = addresses.get(0);
           StringBuilder strReturnedAddress = new StringBuilder("Address:\n");
           for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
            strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
           }
        videoId = (returnedAddress.getAddressLine(1)).toString();
        //sendAddress(videoId);
           //myAddress.setText(strReturnedAddress.toString());
        myAddress.setText("Hello");
          }
          else{
           myAddress.setText("No Address returned!");
          }
         } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
          myAddress.setText("Canont get Address!");

         }
        }




        public void onStatusChanged(String s, int i, Bundle b)
        {
            //Toast.makeText(TouristGuideActivity.this,"Provider status changed",Toast.LENGTH_LONG).show();
        }
        public void onProviderDisabled(String s)
        {
            Toast.makeText(TravelGuide.this,"Provider disabled by the user. GPS turned off",
                    Toast.LENGTH_LONG).show();
        }
        public void onProviderEnabled(String s)
        {
            Toast.makeText(TravelGuide.this,"Provider enabled by the user. GPS turned on",
                    Toast.LENGTH_LONG).show();

        }


     }

}

  • define the your textview objects as global for this class and initiate in onCreate() only after just set the value in location listener implement class – Pratik Feb 03 '12 at 05:39
  • 1
    You are getting location object null,I think. – MKJParekh Feb 03 '12 at 06:41

1 Answers1

0

you are getting locaton object null,try put log in else condition..and the reason is you are fetching last known location which in not available in that phone some how.. so if its true..

Go to this answer here implement that code to get current location...and it will work fine.

If you are using GPS Provider to get Location,Its found that Its not so accurate to return you location object,Network Provider is better option According to me.

Community
  • 1
  • 1
MKJParekh
  • 34,073
  • 11
  • 87
  • 98
  • Sorry for the delay. I was out of here for a few days. Yes Location object is null. Let me try the other code. I will post here once completed. Thanks! – Jobin Tensing T Feb 10 '12 at 11:24