android开发问题:Attempted to access a cursor after it has been closed.
2014-12-17 16:06
441 查看
今天,开发android项目中遇到了一个很奇怪的问题。
问题:项目由一个页面跳转到另一个程序(比如按home键),再回到这个程序,就会奔溃。
有错误提示:Attempted to access a cursor after it has been closed.
问题奇怪的地方有,这个问题在android2.2版本下没有,到了4.0版本以上时就出现了。
自己想了一段时间,实在是想不到哪里有错误,最后在网上找到了答案。
是因为查询数据库时用到了以下函数:
Cursor cusor = activity.managedQuery(uri, projection, "address like ?",new String[]{ strsql },"date desc");
并在之后调用了cusor.close(); 关闭游标,但是android 4.0系统会关闭游标,不用自己手动的去关闭,所以就出现了这个奇怪的问题。
解决的办法是:
原来的代码:
Cursor cusor = activity.managedQuery(uri, projection, "address like ?",new String[]{ strsql },"date desc");
换成了以下代码:
ContentResolver cr = activity.getContentResolver();
Cursor cusor = cr.query(uri, projection, "address like ?",new String[]{ strsql },"date desc");
猿友们也有提供另一种方法,游标不要关闭。
但程序中其他的地方都是用了游标就关闭,这个地方就不关闭,总觉得怪怪的(不知道是不是强迫症)。
还有猿友说,managedQuery会阻塞主线程,已经不推荐使用。
1、觉得这个问题很奇怪。
2、managedQuery会阻塞主线程,已经不推荐使用,想找时间研究研究。
3、项找时间研究研究这个解决办法的原理。
基于以上3个原因,写下此文,希望能够给遇到同样问题的猿友帮助。
问题:项目由一个页面跳转到另一个程序(比如按home键),再回到这个程序,就会奔溃。
有错误提示:Attempted to access a cursor after it has been closed.
问题奇怪的地方有,这个问题在android2.2版本下没有,到了4.0版本以上时就出现了。
自己想了一段时间,实在是想不到哪里有错误,最后在网上找到了答案。
是因为查询数据库时用到了以下函数:
Cursor cusor = activity.managedQuery(uri, projection, "address like ?",new String[]{ strsql },"date desc");
并在之后调用了cusor.close(); 关闭游标,但是android 4.0系统会关闭游标,不用自己手动的去关闭,所以就出现了这个奇怪的问题。
解决的办法是:
原来的代码:
Cursor cusor = activity.managedQuery(uri, projection, "address like ?",new String[]{ strsql },"date desc");
换成了以下代码:
ContentResolver cr = activity.getContentResolver();
Cursor cusor = cr.query(uri, projection, "address like ?",new String[]{ strsql },"date desc");
猿友们也有提供另一种方法,游标不要关闭。
但程序中其他的地方都是用了游标就关闭,这个地方就不关闭,总觉得怪怪的(不知道是不是强迫症)。
还有猿友说,managedQuery会阻塞主线程,已经不推荐使用。
1、觉得这个问题很奇怪。
2、managedQuery会阻塞主线程,已经不推荐使用,想找时间研究研究。
3、项找时间研究研究这个解决办法的原理。
基于以上3个原因,写下此文,希望能够给遇到同样问题的猿友帮助。
相关文章推荐
- android.database.StaleDataException: Attempted to access a cursor after it has been closed
- Android报错 Attempted to access a cursor after it has been closed.的解决方法
- android.database.StaleDataException: Attempted to access a cursor after it has been closed
- Attempted to access a cursor after it has been closed 的问题
- 异常:android.database.StaleDataException: Attempted to access a cursor after it has been closed
- Android读取相册时报错:Attempted to access a cursor after it has been closed.
- android.database.StaleDataException: Attempted to access a cursor after it has been closed.
- Caused by: android.database.StaleDataException: Attempted to access a cursor after it has been close
- android.database.StaleDataException: Attempted to access a cursor after it has been clos
- android.database.StaleDataException: Attempted to access a cursor after it has b
- The object of type 'RectTransform' has been destroyed but you are still trying to access it
- android中Warning: Activity not started, its current task has been brought to the front是什么错误
- android初学之Android异常之 Warning: Activity not started, its current task has been brought to the front
- 安卓问题报告小记(一): Activity not started, its current task has been brought to the front
- 问题: ActivityManager: Warning: Activity not started, its current task has been brought to the front
- ActivityManager: Warning: Activity not started, its current task has been brought to the front 的的问题
- ActivityManager: Warning: Activity not started, its current task has been brought to the front 的的问题
- 安卓问题报告小记(一): Activity not started, its current task has been brought to the front
- ActivityManager: Warning: Activity not started, its current task has been brought to the front 的的问题
- 关于Android开发导入已有项目遇到Project has no default.properties file! Edit the project properties to set one.问题的解决方法