以前一直是在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)
@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)