Geocode index = 0 Problem UND Alter aus Datum berrechnen

  • Antworten:1
Kevin
  • Forum-Beiträge: 12

22.01.2018, 12:45:08 via Website

Hallo Leute,

also für meine App soll ein User eine Adresse oder Ort eingeben können, mit Geocoder sollte mir die Longtitude und Latitude zurück gegeben werden die ich brauche.

Das Problem dabei ist, dass es selten mal funktioniert und dann einfach gar nicht und ich bekomme eine Inboundexception index = 0.

Das gleiche wenn ich den LocationManager benutze und der user per GPS den Standord auswählt.

public String getLocationFromAddress(Context context, String strAddress) {

    Geocoder coder = new Geocoder(context);
    List<Address> address;
    String p1 = null;

    try {
        // May throw an IOException
        address = coder.getFromLocationName(strAddress, 5);
        if (address == null) {
            return null;
        }
        Address location = address.get(0);
        location.getLatitude();
        location.getLongitude();

        p1 = location.getLatitude()+","+location.getLongitude();

    } catch (IOException ex) {

        ex.printStackTrace();
    }

    return p1;
}

Und zum anderen Thema, aus einem Datum herraus, soll das Alter bestimmt werden, soweit so gut, dass Problem das ich jetzt habe, dass wenn jemand zum beispiel am 1. Januar 2018 geboren ist, wird er ein Jahr zu jung dargestellt.

      GregorianCalendar cal = new GregorianCalendar();
    int y, m, d, a;

    y = cal.get(Calendar.YEAR);
    m = cal.get(Calendar.MONTH);
    d = cal.get(Calendar.DAY_OF_MONTH);
    cal.set(year, month, day);
    a = y - cal.get(Calendar.YEAR);
    if ((m < cal.get(Calendar.MONTH))
            || ((m == cal.get(Calendar.MONTH)) && (d < cal
            .get(Calendar.DAY_OF_MONTH)))) {
        --a;
    }
    if(a < 0)
        throw new IllegalArgumentException("Age < 0");
    return String.valueOf(a);


}

Thema alter ist erledigt:

public String getAge(Context context, int day, int month, int year) throws ParseException {

JodaTimeAndroid.init(context);

String date = day + "." + month + "." + year;
DateFormat format = new SimpleDateFormat("dd.mm.yyyy");
Date fo = format.parse(date);

org.joda.time.LocalDate birthday = new org.joda.time.LocalDate(year,month,day);
org.joda.time.LocalDate now = new org.joda.time.LocalDate();

Years age = Years.yearsBetween(birthday,now);

return String.valueOf(age.getYears());

}

Hab die Bibliothek benutzt

Ich hab dahingehend echt viel Probiert aber die Lösung noch nicht gefunden.

Ich danke euch

— geändert am 22.01.2018, 19:39:04

Kommentieren
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

22.01.2018, 21:20:30 via Website

Tritt die IndexOutOfBoundsExecption zufällig hier auf?:

Address location = address.get(0);

Log?

Wenn ja, prüfe bitte mal vorher ab ob address.size()>0, wenn wenn address.size()=0 dann gibt es kein "0." Element daher die Exception wenn du stelle 0 lesen willst, ist aber auch !=null da ja eine leere Liste.

LG Pascal //It's not a bug, it's a feature. :) ;)

Hilfreich?
Kommentieren