CursorIndexOutOfBoundsException

public void initAdapter() {

		dm.open();// 打开数据库操作对象

		cursor = dm.selectAll();// 获取所有数据

		cursor.moveToFirst();// 将游标移动到第一条数据,使用前必须调用

		int count = cursor.getCount();// 个数
		ArrayList<String> items = new ArrayList<String>();//设置记事本的标题数组
		ArrayList<String> times = new ArrayList<String>();//设置记事本记录时间的数组
		for (int i = 0; i < count; i++) {  //通过for不断循环将数据库中取到的数据的时间和标题添加到Listview上
			items.add(cursor.getString(cursor.getColumnIndex("title")));
			times.add(cursor.getString(cursor.getColumnIndex("time")));
			cursor.moveToNext();// 将游标指向下一个
		}
		listcounts = (TextView)findViewById(R.id.list_counts);
		String b = Integer.toString(count);
		listcounts.setText(b);
		dm.close();// 关闭数据操作对象
		adapter = new ListViewAdapter(this, items, times);// 创建数据源
	}

	@Override
	protected void onDestroy() {// 销毁Activity之前,所做的事
		// TODO Auto-generated method stub
		cursor.close();// 关闭游标
		super.onDestroy();
	}
这是我出错的程序块,后来研究返现必须加上
cursor.moveToFirst();// 将游标移动到第一条数据,使用前必须调用

		int count = cursor.getCount();// 个数不然肯定会报错
, 需要加上if (c.moveToFirst()) {}这个判断条件即可

版权声明:本文为博主原创文章,未经博主允许不得转载。(转载请注明出自 AllenCoder)

原文地址:https://www.cnblogs.com/allencoder/p/4830762.html