我在Main Activity中,单击一个ListItem后进行查询,将结果显示在下面这段代码表示的ListActivity中,如果返回到Main Activity中重复查询,两次之后,就会出现异常提示
Finalizing a Cursor that has not been deactivated or closed. database=/mnt/sdcard/businfo.db table=null ,query=select ID as _id, BUSNUM,TIME,PRICE,FORWARDLINE from businfo where BUSNUM like ?"
我知道这是提示我Cursor没有关闭,但是当我用close()关闭后,问题还是依旧,我应该如何改才能不影响反复使用这个cursor进行查询呢?问题代码如下:
public class BusNumInfo extends ListActivity { private TextView tvBusNumber,tvBusTime,tvTicketPrice,tvBusLine,tvNoResult; private String queryValue; private final String DATABASE_PATH = android.os.Environment .getExternalStorageDirectory().getAbsolutePath(); private final String DATABASE_FILENAME = "businfo.db"; private SQLiteDatabase database; private String databaseFilename; private SQLiteDatabase openDatabase() { try { databaseFilename = DATABASE_PATH+"/"+DATABASE_FILENAME; if(!(new File(databaseFilename).exists())) { InputStream is = getResources().openRawResource(R.raw.businfo); FileOutputStream fos = new FileOutputStream(databaseFilename); byte[] buffer = new byte[8192]; int count = 0; while((count=is.read(buffer))>0) { fos.write(buffer, 0, count); } fos.close(); is.close(); } SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(databaseFilename, null); return database; } catch(Exception e) { } return null; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); tvBusNumber = (TextView)findViewById(R.id.tvBusNumber); tvBusTime = (TextView)findViewById(R.id.tvBusTime); tvTicketPrice = (TextView)findViewById(R.id.tvTicketPrice); tvBusLine = (TextView)findViewById(R.id.tvBusLine); Bundle bundle = getIntent().getExtras(); queryValue = bundle.getString("busnumber"); queryValue = queryValue+"%"; database = openDatabase(); String sql = "select ID as _id, BUSNUM,TIME,PRICE,FORWARDLINE from businfo where BUSNUM like ?"; Cursor cursor = database.rawQuery(sql, new String[]{queryValue}); if(cursor.getCount()>0) { String sum = String.valueOf(cursor.getCount()); setTitle("共查询到"+sum+"条记录"); SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(this, R.layout.busnumresult,cursor, new String[] {"BUSNUM","TIME","PRICE","FORWARDLINE"}, new int[] {R.id.tvBusNumber,R.id.tvBusTime,R.id.tvTicketPrice,R.id.tvBusLine}); setListAdapter(simpleCursorAdapter); } else { setTitle("无查询记录"); List<Map<String,String>> noResultList = new ArrayList<Map<String,String>>(); Map<String,String> noResultMap = new HashMap<String,String>(); noResultMap.put("Message", "无相关车次信息,请核对后再查"); noResultList.add(noResultMap); SimpleAdapter simpleAdapter = new SimpleAdapter(this,noResultList, R.layout.noresult,new String[]{"Message"},new int[]{R.id.tvNoResult}); setListAdapter(simpleAdapter); } } }加 了colse后,先clearn整个工程试一试。 明显没管嘛。。SQLiteDatabase.releaseMemory();