安卓:getfromLocationName()返回的大小为0的地址列表 [英] Android: getfromLocationName() returns size 0 in Address List

查看:141
本文介绍了安卓:getfromLocationName()返回的大小为0的地址列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的位置的数据库。当我尝试将它们转换成坐标,这样我就可以把他们在地图上,我得到的错误。我的地址类型AddressList中的大小为零。我研究的话题。我所有的清单权限是否正确。我的手机连接到互联网。我已经重新启动我的电话。我知道有一个谷歌的问题和解决方案,不帮助。该位置是准确的。请大家帮帮忙。

下面是错误信息: android.database.CursorIndexOutOfBoundsException:指数0要求,为0的大小

 私人无效setUpMap(){
    数据库处理器DB =新的数据库处理器(本);
    光标C = db.fetchAllAddresses();
    字符串的地方= c.getString(c.getColumnIndex(名字));
    地理codeR地理codeR =新的地缘codeR(这一点,Locale.getDefault());
    名单< android.location.Address>地址=新的ArrayList();
    尝试 {
       地址=地理coder.getFromLocationName(的地方,1);
    }赶上(IOException异常E){
        e.printStackTrace();
    }

    android.location.Address添加= addresses.get(0);
    双纬度= add.getLatitude();
    双LNG = add.getLongitude();
    (新MarkerOptions()位置(新的经纬度(纬度,经度))标题(标记)。)mMap.addMarker;


    }
 

修改这是fetchAllAddresses()

 公开光标fetchAllAddresses(){
    SQLiteDatabase DB = this.getWritableDatabase();
    光标mCursor = db.query(TABLE_CONTACTS,新的String [] {ROWID _id,KEY_NAME,},NULL,NULL,NULL,NULL,NULL);

    如果(mCursor!= NULL){
        mCursor.moveToFirst();
    }
    返回mCursor;
}
 

解决方案

它看起来像你没有得到你的光标任何结果,而 mCursor.moveToFirst(); 将返回假在这种情况下。

通常情况下,你会想检查的结果mCursor.moveToFirst(); ,因为如果返回false,你知道你有一个空指针。

空指针是一个单独的问题,这是很难说为什么会发生这一code。

为了摆脱你的 CursorIndexOutOfBoundsException 当光标是空的,这是解决它的方法:

您可以做一个检查,以 getCount将()你的光标,以确保它是大于零。 另外,还要确保关闭游标,否则就会有内存泄漏。

 私人无效setUpMap(){
    数据库处理器DB =新的数据库处理器(本);
    光标C = db.fetchAllAddresses();

    //检查空指针
    如果(c.getCount()大于0){
      字符串的地方= c.getString(c.getColumnIndex(名字));
      地理codeR地理codeR =新的地缘codeR(这一点,Locale.getDefault());
      名单< android.location.Address>地址=新的ArrayList();
      尝试 {
         地址=地理coder.getFromLocationName(的地方,1);
      }赶上(IOException异常E){
         e.printStackTrace();
      }

      android.location.Address添加= addresses.get(0);
      双纬度= add.getLatitude();
      双LNG = add.getLongitude();
      (新MarkerOptions()位置(新的经纬度(纬度,经度))标题(标记)。)mMap.addMarker;
    }

    c.close(); //摆脱内存泄漏!

 }
 

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;
}

解决方案

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!

 }

这篇关于安卓:getfromLocationName()返回的大小为0的地址列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆