IndexOutOfBounds - 可能是错误的来源?

时间:2012-08-12 10:03:52

标签: android sqlite

我有自定义列表视图,其中包含通话记录信息(姓名,电话号码,日期和时间,持续时间)。只需单击某个项目,就会出现错误:

  

java.lang.IndexOutOfBoundsException:索引2无效,大小为0

在代码中下面标记的行上。

该应用程序已上市,我没有遇到此错误,也无法想到创建它的方法。我需要帮助才能找出可能的原因。

这是代码:

lv1.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {

        Toast.makeText(Calllogs.this, "short click: " + position, Toast.LENGTH_SHORT).show(); 

        if (arr_calllog_name2.size() > 0) arr_calllog_name2.clear();
        if (arr_calllog_phone2.size() > 0) arr_calllog_phone2.clear();
        if (arr_calllog_type2.size() > 0) arr_calllog_type2.clear();
        if (arr_calllog_duration2.size() > 0) arr_calllog_duration2.clear();
        if (arr_calllog_date2.size() > 0) arr_calllog_date2.clear();
        if (arr_calllog_contactid2.size() > 0) arr_calllog_contactid2.clear();

        HotOrNot info2 = new HotOrNot(Calllogs.this);
        info2.open();

        //Some user selected options like view only the incoming, outgoing calls, sort by alphabet or date... 
        LoadView(); 
        LoadSort(); 
        LoadSortBy();

        if (loadedview == 4) {
            if (loadedsortby == 1) //sort by alphabet
            {
                cursorL = info2.getAllTitlesViewSort2Date(loadedsort, d1, d2);
            }
            else if (loadedsortby == 2) //sort by date
            {
                cursorL = info2.getAllTitlesViewSort4Date(loadedsort, d1, d2);
            }
        } else {
            if (loadedsortby == 1) //sort by alphabet
            {
                cursorL = info2.getAllTitlesViewSort1Date(loadedview, loadedsort, d1, d2);
            }
            else if (loadedsortby == 2) //sort by date
            {
                cursorL = info2.getAllTitlesViewSort3Date(loadedview, loadedsort, d1, d2);
            }
        }

         if (cursorL.moveToFirst()){
             do{
                  arr_calllog_name2.add(cursorL.getString(1));
                  arr_calllog_phone2.add(cursorL.getString(2));
                  arr_calllog_type2.add(cursorL.getString(3));
                  arr_calllog_duration2.add(cursorL.getString(4));
                  arr_calllog_date2.add(cursorL.getString(5));
                  arr_calllog_contactid2.add(cursorL.getString(6));
            }while (cursorL.moveToNext());
        }

        info2.close();

        name = arr_calllog_name2.get(position); //error line
        phone = arr_calllog_phone2.get(position);
        type = arr_calllog_type2.get(position);
        duration = arr_calllog_duration2.get(position);
        date = arr_calllog_date2.get(position);
        contactid = arr_calllog_contactid2.get(position);

        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_DIAL);
        intent.setData(Uri.parse("tel:" + phone));
        startActivity(intent);
    }
});

首先,有一个包含项目的列表视图。用户单击某个项目,然后活动力将关闭。 CursorL在代码的开头定义(Cursor cursorL)。 LoadView(); LoadSort(); LoadSortBy();函数按字母或日期对列表进行排序,仅显示传入和/或传出呼叫等。首先它们必须给出一个值,因为我为它们设置了默认值:

public void LoadView() {
    sharedPreferences = PreferenceManager
            .getDefaultSharedPreferences(getApplicationContext());
    loadedview = sharedPreferences.getInt("view", 4);
}

public void LoadSort() {
    sharedPreferences = PreferenceManager
            .getDefaultSharedPreferences(getApplicationContext());
    loadedsort = sharedPreferences.getString("sort", "DESC");
}

public void LoadSortBy() {
    sharedPreferences = PreferenceManager
            .getDefaultSharedPreferences(getApplicationContext());
    loadedsortby = sharedPreferences.getInt("sortby", 2);
}

loadedsortby变量只能有两个值:1和2,我检查过。 loadedview只能有4个值:1,2,3,4。

我所能想到的只是cursorL,因此arr_calllog_name2 arraylist在某种程度上是空的,但我不知道它是如何可能的。

作为示例,我发布了一个填充cursorL的查询:

public Cursor getAllTitlesViewSort1Date(int view, String sort, String date1, String date2) {
        return ourDatabase.rawQuery("SELECT * FROM " + DATABASE_TABLE + " WHERE " + KEY_DATE + " BETWEEN '" + date1 + "'" + " AND '" + date2 + "'" + " AND " + KEY_TYPE + " = '" + view + "'" + " ORDER BY " + KEY_NAME + " COLLATE NOCASE " + sort, null);
    }

欢迎任何想法。

1 个答案:

答案 0 :(得分:0)

而不是使用这个...

if (cursorL.moveToFirst()){
             do{
                  arr_calllog_name2.add(cursorL.getString(1));
                  arr_calllog_phone2.add(cursorL.getString(2));
                  arr_calllog_type2.add(cursorL.getString(3));
                  arr_calllog_duration2.add(cursorL.getString(4));
                  arr_calllog_date2.add(cursorL.getString(5));
                  arr_calllog_contactid2.add(cursorL.getString(6));
            }while (cursorL.moveToNext());
        }

尝试

cursorL.getCount()

如果计数不为0,那么就这样做......

        cursorL.moveToFirst();
        while(!cursorL.isAfterLast())
        {
            arr_calllog_name2.add(cursorL.getString(1));
                  arr_calllog_phone2.add(cursorL.getString(2));
                  arr_calllog_type2.add(cursorL.getString(3));
                  arr_calllog_duration2.add(cursorL.getString(4));
                  arr_calllog_date2.add(cursorL.getString(5));
                  arr_calllog_contactid2.add(cursorL.getString(6));
            cursorL.moveToNext();
        }
        cursorL.close();