java.lang.NumberFormatException:无效的int:""在Android中的光标

时间:2015-01-12 08:13:12

标签: java android database cursor

我查看了类似的问题,但我没有找到答案。

我收到错误:

java.lang.NumberFormatException: Invalid int: "" 

它在线显示:

    Place place = new Place(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2), cursor.getDouble(3), cursor.getDouble(4), cursor.getInt(5));

这是我的代码:

    Place getMyPlace(String sql, int id){
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(TABLE_PLACES, new String[] {PLACES_COLUMN_ID, PLACES_COLUMN_NAME,
                PLACES_COLUMN_DESCRIPTION}, PLACES_COLUMN_ID + "=?",
                new String[] { String.valueOf(id) }, null, null, null, null);

        if (cursor != null)
            cursor.moveToFirst();

        Place place = new Place(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2), cursor.getDouble(3), cursor.getDouble(4), cursor.getInt(5));
        return place;
    }

它是Place Place的构造函数:

public Place(int id, String placeName, String placeDescription, double latidute, double longtidute, int visited){
    this._id = id;
    this._placeName = placeName;
    this._placeDescription = placeDescription;
    this._latidute = latidute;
    this._longtidute = longtidute;
    this._visited = visited;
}

来自acticity的方法:

public void getPlace(int id){
    DataBaseHelper dbHelper = new DataBaseHelper(this.getApplicationContext());
    String sql ="SELECT Name, Description FROM Places WHERE _id=?" ;
    Place myPlace = dbHelper.getMyPlace(sql, id); 
}

有人知道可能出现的问题吗?

2 个答案:

答案 0 :(得分:2)

更改你的db.query

Cursor cursor = db.query(TABLE_PLACES, new String[] {PLACES_COLUMN_ID, 
            PLACES_COLUMN_NAME, PLACES_COLUMN_DESCRIPTION}, PLACES_COLUMN_ID + "=?",
            new String[] { String.valueOf(id) }, null, null, null, null);

到这个

Cursor cursor = db.query(TABLE_PLACES, null, PLACES_COLUMN_ID + "=?",
            new String[] { String.valueOf(id) }, null, null, null, null);

答案 1 :(得分:1)

数据库中有一个值为空的值:

  Integer.parseInt(cursor.getString(0))

这是您的地方对象的第一个参数。你必须确定,这是一个数字,你可以这样做:

  int numberYouNeed = 0;
  String yourString = cursor.getString(0);

  if(yourString!=null&&!yourString.equals("")){
  numberYouNeed = Integer.parseInt(yourString);
  }

  Place place = new Place(numberYouNeed, cursor.getString(1), cursor.getString(2), cursor.getDouble(3), cursor.getDouble(4), cursor.getInt(5))