得到错误:由以下原因引起:java.lang.ArrayIndexOutOfBoundsException:length = 3;指数= 4

时间:2018-08-20 10:47:49

标签: java android arrays sqlite indexoutofboundsexception

以下代码中出现错误。我试图在SQLite数据库中保存行进距离,但在SQLite数据库中出现错误。我不知道如何管理数组长度。我收到一个名为

的错误
  

java.lang.ArrayIndexOutOfBoundsException:length = 3;索引= 4。

public class ActivityLocationDaoImpl extends Dao implements ActivityLocationDao {

private static final String TABLE_NAME = "activity_location";
private static final String COLUMN_ID = "id";
private static final String COLUMN_LATITUDE = "latitude";
private static final String COLUMN_LONGITUDE = "longitude";
private static final String COLUMN_ACTIVITY = "activity";
private static final String COLUMN_DATE = "date";
private static final String[] COLUMN_ARRAY = new String[]{COLUMN_ID, COLUMN_LATITUDE, COLUMN_LONGITUDE, COLUMN_ACTIVITY, COLUMN_DATE};
public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (\n" +
        COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,\n" +
        COLUMN_LATITUDE + " INTEGER NOT NULL,\n" +
        COLUMN_LONGITUDE + " INTEGER NOT NULL,\n" +
        COLUMN_ACTIVITY + " INTEGER NOT NULL,\n" +
        COLUMN_DATE + " INTEGER NOT NULL\n" + ")";

public ActivityLocationDaoImpl(SQLiteDatabase database) {
    super(database);
}

@Override
public boolean insert(ActivityLocation activityLocation) {
    ContentValues contentValues = new ContentValues();
    contentValues.put(COLUMN_LATITUDE, activityLocation.getLocation().getLatitude());
    contentValues.put(COLUMN_LONGITUDE, activityLocation.getLocation().getLongitude());
    contentValues.put(COLUMN_ACTIVITY, activityLocation.getActivityType().getIndex());
    contentValues.put(COLUMN_DATE, activityLocation.getDate().getTime());

    return getDatabase().insert(TABLE_NAME, null, contentValues) != -1;
}

@Override
public List<ActivityLocation> listAll(Date currentDay) {

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(currentDay);
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);

    currentDay = calendar.getTime();
    calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + 1);
    Date nextDay = calendar.getTime();

    Cursor cursor = getDatabase().query(TABLE_NAME, COLUMN_ARRAY, COLUMN_DATE + " > ? AND " + COLUMN_DATE + " < ?",
            new String[]{String.valueOf(currentDay.getTime()), String.valueOf(nextDay.getTime())},
            null, null, COLUMN_DATE + " ASC", null);

    List<ActivityLocation> activityLocationList = new ArrayList<>();
    while (cursor.moveToNext()) {
        activityLocationList.add(convertCursorToEntity(cursor));
    }
    return activityLocationList;
}

@Override
public List<ActivityLocation> listAll(Date startDate, Date finalDate) {

    Cursor cursor = getDatabase().query(TABLE_NAME, COLUMN_ARRAY, COLUMN_DATE + " > ? AND " + COLUMN_DATE + " < ?",
            new String[]{String.valueOf(startDate.getTime()), String.valueOf(finalDate.getTime())},
            null, null, COLUMN_DATE + " ASC", null);

    List<ActivityLocation> activityLocationList = new ArrayList<>();
    while (cursor.moveToNext()) {
        activityLocationList.add(convertCursorToEntity(cursor));
    }
    return activityLocationList;
}

public ActivityLocation convertCursorToEntity(Cursor cursor) {
    ActivityLocation activityLocation = new ActivityLocation();
    Location location = new Location();

    activityLocation.setId(cursor.getInt(0));
    location.setLatitude(cursor.getDouble(1));
    location.setLongitude(cursor.getDouble(2));
    activityLocation.setLocation(location);
    activityLocation.setActivityType(ActivityType.values()[cursor.getInt(3)]);
    activityLocation.setDate(new Date(cursor.getLong(4)));

    return activityLocation;
}

在此行出现错误:

  

activityLocation.setActivityType(ActivityType.values()[cursor.getInt(3)]);

谢谢。

3 个答案:

答案 0 :(得分:0)

很可能,游标仅包含3个结果列。因此,您只能访问0、1和2。

答案 1 :(得分:0)

那么,您不能访问只有3个元素的数组的第4个元素。通常,人们将其与C / C ++的背景知识相混淆。 这是C / C ++和JAVA的区别。 假设您有这样的东西:

int arr[] = {1,2,3};

现在,如果您在C / C ++中执行arr [3],它将返回一些垃圾值,无论该特定地址处的值是什么,但是在JAVA中,将抛出ArrayIndexOutOfBoundsException。

答案 2 :(得分:0)

Shravan,只有3个值时,无法从数组中获取第4个索引值。

您只能访问索引值为0,1,2。

在崩溃行上使用断点,您将获得清晰的画面。