at android.database.sqlite.SQLiteStatement.releaseAndUnlock(SQLiteStatement.

     项目中有一个删除的动作,需要删除文件以及文件在数据库中的记录,我在删除前进行了一次数据库的查询操作,然后下面进行了删除的动作,报出了这个错误

/**
	 * 从数据库中删除下载任务
	 * 
	 * @param downLoadItems
	 */
	public void deleteDownLoadedFromDB(List<DownLoadItem> downLoadItems) {
		
		SQLiteDatabase db = dbTool.getWritableDatabase();
		for (DownLoadItem downLoadItem : downLoadItems) {
			if (downLoadItem.isDirectory()) {
//				FIXME 删除的时候把下载中的也删掉了
				List<DownLoadItem> downLoadItems2 = getEpgListByTvId(downLoadItem.getTvAndEpg().getTv().getId()+"");
				db = dbTool.getWritableDatabase();//FIXME 加上这段代码解决了问题
				for (DownLoadItem downLoadItem2 : downLoadItems2) {
					db.execSQL("DELETE FROM loadtask WHERE epgid=?", new String[]{downLoadItem2.getTvAndEpg().getEpg().getId()+""});
				}
				
			}else{
				db.execSQL("DELETE FROM loadtask where loadid=?",
						new String[] { downLoadItem.getLoadId() });
			}			
		}
		db.close();
	}
	


 

/**
	 * 得到已下载的某一个tv内的epg
	 * @param tv_Id
	 * @return
	 */
	public List<DownLoadItem> getEpgListByTvId(String tv_Id) {
		Log.i("III", "进入查询已下载条目代码块");
		List<DownLoadItem> downLoadItems = new ArrayList<DownLoadItem>();
		SQLiteDatabase db = dbTool.getReadableDatabase();
		Cursor cursor = null;
		if (null == tv_Id) {
			System.out.println("NULL");
			cursor = db
					.rawQuery(
							"select tvid,count(tvid)as counts,tvname,epgdata,epgstarttime,epgendtime,epgeventname,tvimagelink,epgid,downloadlink from (select * from loadtask where loadstatue='3') group by tvid",
							null);
		} else {
			System.out.println("NOTNULL");
			cursor = db
					.rawQuery(
							"select tvid,tvname,epgdata,epgstarttime,epgendtime,epgeventname,tvimagelink,epgid,downloadlink from loadtask where loadstatue='3' and tvid=?",
							new String[] { tv_Id });
		}
		while (cursor.moveToNext()) {
			DownLoadItem downLoadItem = new DownLoadItem();
			TvAndEpg tvAndEpg = new TvAndEpg();
			Tv tv = new Tv();
			Epg epg = new Epg();

			String count;
			try {
				count = cursor
						.getString(cursor.getColumnIndexOrThrow("counts"));// 当使用tvid查找epgList时会没有该字段,把该字段的值设置为1
			} catch (IllegalArgumentException e) {
				count = "1";
			}

			int tvId = cursor.getInt(cursor.getColumnIndex("tvid"));
			String tvName = cursor.getString(cursor.getColumnIndex("tvname"));
			String tvImageUrl = cursor.getString(cursor
					.getColumnIndex("tvimagelink"));
			/*Log.i("UUU", "tvID" + tvId + " tvName :" + tvName + " tvImageUrl: "
					+ tvImageUrl);*/
			tv.setId(tvId);
			tv.setName(tvName);
			tv.setImageLink(tvImageUrl);

			String epgName = cursor.getString(cursor
					.getColumnIndex("epgeventname"));
			int epgId = cursor.getInt(cursor.getColumnIndex("epgid"));
			String date = cursor.getString(cursor.getColumnIndex("epgdata"));
			String startTime = cursor.getString(cursor
					.getColumnIndex("epgstarttime"));
			String endTime = cursor.getString(cursor
					.getColumnIndex("epgendtime"));
			epg.setEventName(epgName);
			epg.setId(epgId);
			epg.setDate(date);
			epg.setStartTime(startTime);
			epg.setEndTime(endTime);

			String downLoadLink = cursor.getString(cursor
					.getColumnIndex("downloadlink"));

			tvAndEpg.setTv(tv);
			tvAndEpg.setEpg(epg);

			downLoadItem.setLoadstatue(3);// FIXME
			downLoadItem.setDownLoadLink(downLoadLink);
			downLoadItem.setCount(Integer.parseInt(count));
			downLoadItem.setTvAndEpg(tvAndEpg);
			downLoadItems.add(downLoadItem);
			downLoadItem = null;
		}
		if (cursor != null) {
			cursor.close();
		}
		db.close();
		return downLoadItems;
	}


 

研究发现getEpgListByTvId这个方法执行完毕后关闭了数据库,而在deleteDownLoadedFromDB这个方法中的数据库定义是在getEpgListByTvId方法之前,所以可能是把deleteDownLoadedFromDB这个方法中的数据库也关闭了,所以我重新获取了一下数据库解决了问题,特此记录一下。

你可能感兴趣的:(android,数据空指针异常,操作频繁)