Android4.X数据库的cursor问题

以前一直是在Android2.3平台练习,但是将程序运行在Android4.X平台上时就出现各种问题,不论是网络的还是数据库的,不得不说Android4.X跟Android2.3还是有很大的不同的,在写代码时更要有严谨性,这几天遇到的问题是数据库的cursor问题,将数据库的内容以二级列表的形式显示,并且可单击列表查看详细信息,即在Activity A中显示列表,当单击某一个子列表时进入Activity B来显示详细信息,用cursor存储从数据库得到的数据。问题表现为:当从Activity B返回到Activity A时,程序异常退出

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to resume activity {***}: java.lang.IllegalStateException: trying to requery an already closed cursor  android.database.sqlite.SQLiteCursor@411c86d0
	at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2568)
	at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2596)
	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1184)
	at android.os.Handler.dispatchMessage(Handler.java:99)
	at android.os.Looper.loop(Looper.java:137)
	at android.app.ActivityThread.main(ActivityThread.java:4586)
	at java.lang.reflect.Method.invokeNative(Native Method)
	at java.lang.reflect.Method.invoke(Method.java:511)
	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
	at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: trying to requery an already closed cursor  android.database.sqlite.SQLiteCursor@411c86d0
	at android.app.Activity.performRestart(Activity.java:4508)
	at android.app.Activity.performResume(Activity.java:4531)
	at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2558)

网上查找了一下,说是startManagingCursor()函数的问题,在Build.VERSION.SDK_INT小于11时用这个函数没有问题,当大于11时,就不用这个函数了,因此,按照网上的代码,覆写startManagingCursor(Cursor c)函数,如下:

@Override
public void startManagingCursor(Cursor c) {

    // To solve the following error for honeycomb:
    // java.lang.RuntimeException: Unable to resume activity 
    // java.lang.IllegalStateException: trying to requery an already closed cursor
    if (Build.VERSION.SDK_INT < VersionUtil.HONEYCOMB) {
        super.startManagingCursor(c);
    }
}
满怀希望地运行代码,结果又出现了其他问题

05-17 11:11:03.163: E/StrictMode(27580): Finalizing a Cursor that has not been deactivated or closed. 
05-17 11:11:03.163: E/StrictMode(27580): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
05-17 11:11:03.163: E/StrictMode(27580): 	at android.database.sqlite.SQLiteCursor.<init>(SQLiteCursor.java:99)
05-17 11:11:03.163: E/StrictMode(27580): 	at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:51)
05-17 11:11:03.163: E/StrictMode(27580): 	at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1564)
05-17 11:11:03.163: E/StrictMode(27580): 	at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1449)
05-17 11:11:03.163: E/StrictMode(27580): 	at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1405)
05-17 11:11:03.163: E/StrictMode(27580): 	at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1485)

纠结的根源还是cursor的问题,又查看了下跟cursor有关的代码,看不出有啥问题,最后觉得应该是Activity切换的问题,跟二级列表有关,解决办法是在从Activity A切换到Activity B时调用stopManagingCursor(cursor)函数,切换回Activity A时再重新获取cursor,并管理。不知各位有啥更好的办法解决。。。

你可能感兴趣的:(Android4.X数据库的cursor问题)