0

I have a Geocoder and it works when I put a longitude and latitude in programically but when I try to put in the latitude and longitude from user location it force closes this is what I have

 public String address(){
    Geocoder gc = new Geocoder(this);


      List<Address> list = null;
    try {
        list = gc.getFromLocation(latt(),longg(),1);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

      Address address = list.get(0);

      StringBuffer str = new StringBuffer();
      str.append("" + address.getLocality() + "\n");
      str.append("" + address.getSubAdminArea() + "\n");
      str.append("" + address.getAdminArea() + "\n");
      str.append("" + address.getCountryName() + "\n");
      str.append("" + address.getCountryCode() + "\n");

      String strAddress = str.toString();


    return strAddress;
    }

public double latt(){
return LocationManagerHelper.getLatitude() ;
}
public double longg(){
    return LocationManagerHelper.getLongitude();
}

Logcat

 10-26 20:15:11.936: E/AndroidRuntime(28800):   at http.www.hotapp.com.timeandlocation.TimeAndLocationActivity.address(TimeAndLocationActivity.java:95)

line 95 is Address address = list.get(0);

any suggestions on what i am doing wrong?

Later Question

My thoughts are that the numbers of the latitude and longitude are to long when they get pulled in. So my question is, is there anyway to make it so that the long and lat come in as a smaller digit? When I programicaly put in the long and lat its a shorter number latitude: 43.47389 Longitude:-114.34639. When it is pulling from user location i get Latitude: 43.53400643333333 longitude: -114.32474341666666. Even though When I put in the longer digits programically it still works fine.

Christian
  • 958
  • 5
  • 23
  • 52

1 Answers1

0

Since your app is crashing at list.get(0) I'm guessing it's a NullPointerException. And since list is getting initiated inside a try block and you're eating the exception, looking into why gc.getFromLocation(latt(),longg(),1); fails would get you closer to a solution.

Keep in mind that code like this only puts off the problem and will make it harder to find later:

catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
}

Failing fast is usually a good policy.


As to why gc.getFromLocation(latt(),longg(),1); is failing, the documentation says that the IOException happens "if the network is unavailable or any other I/O problem occurs". Is the network setup properly? Does the device that you're running this on have the proper GeoCoding services available? The emulator does not.

Community
  • 1
  • 1
spatulamania
  • 6,613
  • 2
  • 30
  • 26
  • well when I put `gc.getFromLocation(43.47389,-114.34639,1);` it works just how I want it as soon as I try to switch to get from the user location it forcecloses. so I am guessing that when I call from the user location that the coordenets are to long. how would I make it so that the latitude and longitude get put in as a smaller veriable? – Christian Oct 27 '11 at 03:00
  • @Christian use some simple `if` checks to see if their out of the acceptable range. – spatulamania Oct 27 '11 at 03:03
  • I am not exactly sure what you mean with the `if` – Christian Oct 27 '11 at 03:05