Android: getfromLocationName() returns size 0 in Address List

 ̄綄美尐妖づ 提交于 2019-12-02 06:24:38

问题


I have a database of locations. When I try to convert them into coordinates so I can place them on a map I get errors. My addresslist of type address has a size zero. I researched the topic. All my manifest permissions are correct. My phone is connected to the internet. I have rebooted my phone. I know there is an google issue and the solutions dont help. The locations are accurate. Please help.

Here is error message : android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0

private void setUpMap()  {
    DatabaseHandler db = new DatabaseHandler(this);
    Cursor c  = db.fetchAllAddresses();
    String place = c.getString(c.getColumnIndex("name"));
    Geocoder geocoder = new Geocoder(this, Locale.getDefault());
    List<android.location.Address> addresses = new ArrayList();
    try {
       addresses = geocoder.getFromLocationName(place,1);
    } catch (IOException e) {
        e.printStackTrace();
    }

    android.location.Address add = addresses.get(0);
    double lat = add.getLatitude();
    double lng = add.getLongitude();
    mMap.addMarker(new MarkerOptions().position(new LatLng(lat,lng)).title("Marker"));


    }

EDIT this is the fetchAllAddresses()

  public Cursor fetchAllAddresses() {
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor mCursor = db.query(TABLE_CONTACTS, new String[]{ "rowid _id", KEY_NAME,},null, null, null,null, null);

    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}

回答1:


It looks like you're not getting any results in your cursor, and mCursor.moveToFirst(); is returning false in this case.

Typically you would want to check the result of mCursor.moveToFirst();, as if it returns false, you know that you have an empty cursor.

The empty cursor is a separate issue, it's hard to tell why that would happen from this code.

In order to get rid of your CursorIndexOutOfBoundsException when the Cursor is empty, this is one way to fix it:

You can do a check to getCount() on your cursor to make sure it's greater than zero. Also make sure to close the cursor, otherwise you will have a memory leak.

private void setUpMap()  {
    DatabaseHandler db = new DatabaseHandler(this);
    Cursor c  = db.fetchAllAddresses();

    //check for empty cursor
    if (c.getCount() > 0){
      String place = c.getString(c.getColumnIndex("name"));
      Geocoder geocoder = new Geocoder(this, Locale.getDefault());
      List<android.location.Address> addresses = new ArrayList();
      try {
         addresses = geocoder.getFromLocationName(place,1);
      } catch (IOException e) {
         e.printStackTrace();
      }

      android.location.Address add = addresses.get(0);
      double lat = add.getLatitude();
      double lng = add.getLongitude();
      mMap.addMarker(new MarkerOptions().position(new LatLng(lat,lng)).title("Marker"));
    }

    c.close();  //get rid of memory leak!

 }


来源:https://stackoverflow.com/questions/29463160/android-getfromlocationname-returns-size-0-in-address-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!