0

我有一个带有通话记录信息(姓名、电话号码、日期和时间、持续时间)的自定义列表视图。单击某个项目有时会出现错误:

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数组列表是空的,但我不知道它是怎么可能的。

作为示例,我发布了填充 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);
    }

欢迎任何想法。

4

1 回答 1

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();
于 2012-08-13T03:10:20.303 回答