2013-01-31 2 views
0

J'essaie de récupérer les données de ma base de données via un objet curseur une fois que la base de données a été interrogée. Il semble que j'obtiens une erreur sur mon cursor.close() et lors de la fermeture de ma base de données. Cela a été très bien jusqu'à ce point, mais j'ai commencé à recevoir des erreurs «curseur finalizer» lors de l'accès à des classes de différentes intentions.Curseur finaliser. close() issue

Pour l'instant, il semble que les objets de mon curseur se ferment de manière incorrecte.

Par exemple, voici comment j'essaie de récupérer toutes les données pour remplir une liste dans la méthode onCreate de ma classe.

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 


setContentView(R.layout.appointmentview); 


    searchedAppView = (ListView)findViewById(android.R.id.list); 

    DBHandlerApp DBAppointments = new DBHandlerApp(this, null, null); 

    DBHandlerApp searchApps = new DBHandlerApp(this, null, null); 

    searchApps.open(); 


    Cursor cursor = searchApps.getAppointmentsData(); 

    startManagingCursor(cursor); 


    @SuppressWarnings("static-access") 
    String [] from = new String [] {DBAppointments.KEY_NAMEAPP, DBAppointments.KEY_TYPEAPP, DBAppointments.KEY_TIMEAPP, DBAppointments.KEY_DATEAPP, DBAppointments.KEY_COMMENTAPP}; 
    int [] to = new int [] {R.id.txtAppointName, R.id.txtAppointType, R.id.txtAppointTime, R.id.txtAppointDate, R.id.txtAppointCom}; 

    @SuppressWarnings("deprecation") 

    SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.setappointviews, cursor, from, to); 
    searchedAppView.setAdapter(cursorAdapter); 



    searchAppoints = (ImageButton) findViewById(R.id.btnSearchAppointName); 
    searchAppName = (EditText) findViewById(R.id.inputAppointName); 


    searchAppoints.setOnClickListener(this); 

} 

Heres la méthode 'getAppointmentData' de la classe DBHandlerApp:

public Cursor getAppointmentsData() { 
     String [] columns = new String[]{KEY_ROWAPPID, KEY_NAMEAPP, KEY_TYPEAPP, KEY_TIMEAPP, KEY_DATEAPP, KEY_COMMENTAPP}; 
     Cursor c = ourDatabase.query(DATABASE_TABLEAPP, columns, null, null, null, null, KEY_NAMEAPP + " ASC", null); 

        //*****Closing the cursor object.***** 
     c.close(); 
     return c; 

erreur Logcat:

01-31 17:44:34.220: E/AndroidRuntime(269): FATAL EXCEPTION: main 
01-31 17:44:34.220: E/AndroidRuntime(269): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.flybase2/com.example.flybase2.ViewAppointments}: java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery (mSql = SELECT _id, app_name, app_type, app_time, app_date, app_comments FROM appointmentsTable ORDER BY app_name ASC) 
01-31 17:44:34.220: E/AndroidRuntime(269): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
01-31 17:44:34.220: E/AndroidRuntime(269): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
01-31 17:44:34.220: E/AndroidRuntime(269): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
01-31 17:44:34.220: E/AndroidRuntime(269): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
01-31 17:44:34.220: E/AndroidRuntime(269): at android.os.Handler.dispatchMessage(Handler.java:99) 
01-31 17:44:34.220: E/AndroidRuntime(269): at android.os.Looper.loop(Looper.java:123) 
01-31 17:44:34.220: E/AndroidRuntime(269): at android.app.ActivityThread.main(ActivityThread.java:4627) 
01-31 17:44:34.220: E/AndroidRuntime(269): at java.lang.reflect.Method.invokeNative(Native Method) 
01-31 17:44:34.220: E/AndroidRuntime(269): at java.lang.reflect.Method.invoke(Method.java:521) 
01-31 17:44:34.220: E/AndroidRuntime(269): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
01-31 17:44:34.220: E/AndroidRuntime(269): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
01-31 17:44:34.220: E/AndroidRuntime(269): at dalvik.system.NativeStart.main(Native Method) 
01-31 17:44:34.220: E/AndroidRuntime(269): Caused by: java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery (mSql = SELECT _id, app_name, app_type, app_time, app_date, app_comments FROM appointmentsTable ORDER BY app_name ASC) 
01-31 17:44:34.220: E/AndroidRuntime(269): at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:34) 
01-31 17:44:34.220: E/AndroidRuntime(269): at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:64) 
01-31 17:44:34.220: E/AndroidRuntime(269): at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:283) 
01-31 17:44:34.220: E/AndroidRuntime(269): at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:264) 
01-31 17:44:34.220: E/AndroidRuntime(269): at android.support.v4.widget.CursorAdapter.getCount(CursorAdapter.java:202) 
01-31 17:44:34.220: E/AndroidRuntime(269): at android.widget.ListView.setAdapter(ListView.java:436) 
01-31 17:44:34.220: E/AndroidRuntime(269): at com.example.flybase2.ViewAppointments.onCreate(ViewAppointments.java:54) 
01-31 17:44:34.220: E/AndroidRuntime(269): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
01-31 17:44:34.220: E/AndroidRuntime(269): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
01-31 17:44:34.220: E/AndroidRuntime(269): ... 11 more 

Actuellement au moment où je ne fermez pas la base de données à l'aide .searchApps.close() ;. Si cela je reçois l'erreur logcat suivante:

01-31 17:52:17.324: E/AndroidRuntime(277): FATAL EXCEPTION: main 
01-31 17:52:17.324: E/AndroidRuntime(277): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.flybase2/com.example.flybase2.ViewAppointments}: java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery (mSql = SELECT _id, app_name, app_type, app_time, app_date, app_comments FROM appointmentsTable ORDER BY app_name ASC) 
01-31 17:52:17.324: E/AndroidRuntime(277): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
01-31 17:52:17.324: E/AndroidRuntime(277): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
01-31 17:52:17.324: E/AndroidRuntime(277): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
01-31 17:52:17.324: E/AndroidRuntime(277): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
01-31 17:52:17.324: E/AndroidRuntime(277): at android.os.Handler.dispatchMessage(Handler.java:99) 
01-31 17:52:17.324: E/AndroidRuntime(277): at android.os.Looper.loop(Looper.java:123) 
01-31 17:52:17.324: E/AndroidRuntime(277): at android.app.ActivityThread.main(ActivityThread.java:4627) 
01-31 17:52:17.324: E/AndroidRuntime(277): at java.lang.reflect.Method.invokeNative(Native Method) 
01-31 17:52:17.324: E/AndroidRuntime(277): at java.lang.reflect.Method.invoke(Method.java:521) 
01-31 17:52:17.324: E/AndroidRuntime(277): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
01-31 17:52:17.324: E/AndroidRuntime(277): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
01-31 17:52:17.324: E/AndroidRuntime(277): at dalvik.system.NativeStart.main(Native Method) 
01-31 17:52:17.324: E/AndroidRuntime(277): Caused by: java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery (mSql = SELECT _id, app_name, app_type, app_time, app_date, app_comments FROM appointmentsTable ORDER BY app_name ASC) 
01-31 17:52:17.324: E/AndroidRuntime(277): at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:34) 
01-31 17:52:17.324: E/AndroidRuntime(277): at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:64) 
01-31 17:52:17.324: E/AndroidRuntime(277): at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:283) 
01-31 17:52:17.324: E/AndroidRuntime(277): at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:264) 
01-31 17:52:17.324: E/AndroidRuntime(277): at android.support.v4.widget.CursorAdapter.getCount(CursorAdapter.java:202) 
01-31 17:52:17.324: E/AndroidRuntime(277): at android.widget.ListView.setAdapter(ListView.java:436) 
01-31 17:52:17.324: E/AndroidRuntime(277): at com.example.flybase2.ViewAppointments.onCreate(ViewAppointments.java:56) 
01-31 17:52:17.324: E/AndroidRuntime(277): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
01-31 17:52:17.324: E/AndroidRuntime(277): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
01-31 17:52:17.324: E/AndroidRuntime(277): ... 11 more 

Répondre

1

Vous fermez votre curseur au mauvais endroit. Vous devriez fermer un curseur quand vous avez fini de l'utiliser, vous le fermez juste après l'avoir rempli, avant de le retourner de votre méthode getAppointmentData.

Il suffit de supprimer l'appel close de cette méthode pour résoudre votre problème, car lorsque vous utilisez startManagingCursor, l'activité gère la fermeture. Par ailleurs, startManagingCursor est déprécié en faveur de l'utilisation de Loaders.

+0

Un grand merci pour votre réponse. J'ai enlevé la méthode cursor.close() et cela fonctionne. Si je sélectionne un élément, charge l'intention, puis appuie sur la flèche de retour et re-sélectionne un élément à nouveau Je reçois l'erreur de «Finalisation d'un curseur qui n'a pas été désactivé ou fermé. Est-ce dû au fait que 'startManagingCursor' ne ferme pas correctement mon curseur car il est déprécié? – user1352057

+0

Ce pourrait être une erreur de votre nouvelle activité, ce serait ma conjecture. Je suggère de passer à l'utilisation d'un chargeur, il faut un peu pour le comprendre, mais une fois que vous faites c'est plus facile. Ne pas avoir à s'inquiéter beaucoup des curseurs. – Ralgha

+1

user1352057, s'il vous plaît ne pas oublier de marquer la réponse de Ralgha comme * la * réponse et de voter pour elle! –