Autore Topic: errore con il cursore  (Letto 456 volte)

Offline racoonlab

  • Utente junior
  • **
  • Post: 98
  • Respect: +1
    • Mostra profilo
  • Dispositivo Android:
    HTC Desire
errore con il cursore
« il: 09 Giugno 2011, 12:42:09 CEST »
0
Ciao a tutti,

Ho aperto un post precedente simile, che e' stato risolto. Non capisco dove sia l'arcano su quest'altro codice che ho cercato di risolvere nello stesso modo del precedente, ottenendo invece un errore.

Se dalla activity 1 passo alla 2 attraverso l'uso di un menu e faccio lo scroll nella seconda activity, tutto mi funziona bene. Ritorno alla 1 per poi passare alla seconda e appena scrollo parte l'errore.

Qualcuno mi puo' aiutare su questa cosa? I cursori sto giro sono stati chiusi. Ho provato pure a chiudere il cursore "Cursor cursorListRegion = (Cursor) l.getItemAtPosition(position);" ma niente...

Codice: [Seleziona]
06-08 14:53:54.502: WARN/SQLiteCompiledSql(6242): Releasing statement in a finalizer. Please ensure that you explicitly call close() on your cursor: SELECT _id, name, region FROM mystery GROUP BY region
06-08 14:53:54.502: WARN/SQLiteCompiledSql(6242): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
06-08 14:53:54.502: WARN/SQLiteCompiledSql(6242):     at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:62)
06-08 14:53:54.502: WARN/SQLiteCompiledSql(6242):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:80)
06-08 14:53:54.502: WARN/SQLiteCompiledSql(6242):     at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:46)
06-08 14:53:54.502: WARN/SQLiteCompiledSql(6242):     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
06-08 14:53:54.502: WARN/SQLiteCompiledSql(6242):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1345)
06-08 14:53:54.502: WARN/SQLiteCompiledSql(6242):     at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1229)
06-08 14:53:54.502: WARN/SQLiteCompiledSql(6242):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1184)
06-08 14:53:54.502: WARN/SQLiteCompiledSql(6242):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1264)
06-08 14:53:54.502: WARN/SQLiteCompiledSql(6242):     at com.racoonlab.geo.map.GeoDbAdapter.getRegion(GeoDbAdapter.java:286)
06-08 14:53:54.502: WARN/SQLiteCompiledSql(6242):     at com.racoonlab.geo.map.RegionView.viewRegionList(RegionView.java:34)
06-08 14:53:54.502: WARN/SQLiteCompiledSql(6242):     at com.racoonlab.geo.map.RegionView.onCreate(RegionView.java:28)
06-08 14:53:54.502: WARN/SQLiteCompiledSql(6242):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-08 14:53:54.502: WARN/SQLiteCompiledSql(6242):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-08 14:53:54.502: WARN/SQLiteCompiledSql(6242):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-08 14:53:54.502: WARN/SQLiteCompiledSql(6242):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-08 14:53:54.502: WARN/SQLiteCompiledSql(6242):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-08 14:53:54.502: WARN/SQLiteCompiledSql(6242):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-08 14:53:54.502: WARN/SQLiteCompiledSql(6242):     at android.os.Looper.loop(Looper.java:123)
06-08 14:53:54.502: WARN/SQLiteCompiledSql(6242):     at android.app.ActivityThread.main(ActivityThread.java:4627)
06-08 14:53:54.502: WARN/SQLiteCompiledSql(6242):     at java.lang.reflect.Method.invokeNative(Native Method)
06-08 14:53:54.502: WARN/SQLiteCompiledSql(6242):     at java.lang.reflect.Method.invoke(Method.java:521)
06-08 14:53:54.502: WARN/SQLiteCompiledSql(6242):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-08 14:53:54.502: WARN/SQLiteCompiledSql(6242):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-08 14:53:54.502: WARN/SQLiteCompiledSql(6242):     at dalvik.system.NativeStart.main(Native Method)
06-08 14:53:54.572: ERROR/Database(6242): close() was never explicitly called on database '/data/data/com.racoonlab.geo.map/databases/data'
06-08 14:53:54.572: ERROR/Database(6242): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
06-08 14:53:54.572: ERROR/Database(6242):     at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1810)
06-08 14:53:54.572: ERROR/Database(6242):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:817)
06-08 14:53:54.572: ERROR/Database(6242):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:851)
06-08 14:53:54.572: ERROR/Database(6242):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:844)
06-08 14:53:54.572: ERROR/Database(6242):     at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:540)
06-08 14:53:54.572: ERROR/Database(6242):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
06-08 14:53:54.572: ERROR/Database(6242):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98)
06-08 14:53:54.572: ERROR/Database(6242):     at com.racoonlab.geo.map.GeoDbAdapter.open(GeoDbAdapter.java:109)
06-08 14:53:54.572: ERROR/Database(6242):     at com.racoonlab.geo.map.RegionView.onCreate(RegionView.java:26)
06-08 14:53:54.572: ERROR/Database(6242):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-08 14:53:54.572: ERROR/Database(6242):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-08 14:53:54.572: ERROR/Database(6242):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-08 14:53:54.572: ERROR/Database(6242):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-08 14:53:54.572: ERROR/Database(6242):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-08 14:53:54.572: ERROR/Database(6242):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-08 14:53:54.572: ERROR/Database(6242):     at android.os.Looper.loop(Looper.java:123)
06-08 14:53:54.572: ERROR/Database(6242):     at android.app.ActivityThread.main(ActivityThread.java:4627)
06-08 14:53:54.572: ERROR/Database(6242):     at java.lang.reflect.Method.invokeNative(Native Method)
06-08 14:53:54.572: ERROR/Database(6242):     at java.lang.reflect.Method.invoke(Method.java:521)
06-08 14:53:54.572: ERROR/Database(6242):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-08 14:53:54.572: ERROR/Database(6242):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-08 14:53:54.572: ERROR/Database(6242):     at dalvik.system.NativeStart.main(Native Method)


mentre il semplice codice e' questo
Codice (Java): [Seleziona]
package com.racoonlab.geo.map;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class RegionView extends ListActivity {
        private GeoDbAdapter mDbH;
        private static final String LOG_TAG = "Racoonlab";
        public SimpleCursorAdapter regionList;
        Cursor regionCursor;

        /** Called when the activity is first created. */
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                mDbH = new GeoDbAdapter(this);
                mDbH.open();
                viewRegionList();
        }
       
        public void viewRegionList(){
                regionCursor = mDbH.getRegion();
        startManagingCursor(regionCursor);

        String[] names = new String[] { GeoDbAdapter.KEY_REG };
        int[] to = new int[] { R.id.label };
       
                // Use your own layout and point the adapter to the UI elements which
                // contains the label
                //this.setListAdapter(new ArrayAdapter<String>(this, R.layout.rowlayout, R.id.label, names));
                SimpleCursorAdapter regionList = new SimpleCursorAdapter(this, R.layout.rowlayout, regionCursor, names, to);
               
        setListAdapter(regionList);
                // Create an ArrayAdapter, that will actually make the Strings above
                // appear in the ListView
                //without icons
                //this.setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, names));
       
        }
       
        @Override
        protected void onListItemClick(ListView l, View v, int position, long id) {
                super.onListItemClick(l, v, position, id);
                // use the position to set a cursor of that position
            Cursor cursorListRegion = (Cursor) l.getItemAtPosition(position);
            String itemId = cursorListRegion.getString(7) ;
           
                //Toast.makeText(this, selection, Toast.LENGTH_LONG).show();
                //Log.v(getClass().getSimpleName(), "id=" + id);
                Log.v(LOG_TAG, "Inside onListItem ");
               
                Intent cityIntent = new Intent(this, CityView.class);
                cityIntent.putExtra("regionCode", itemId);
                // callback via this code
               
        this.startActivity(cityIntent);
        }
       
        @Override
        public void onResume() {
                super.onResume();
                regionCursor.requery();
        }
        @Override
        public void onPause() {
                super.onPause();
                regionCursor.close();
        }
       
        @Override
        public void onDestroy() {
                super.onDestroy();
                regionCursor.close();
        }

}
« Ultima modifica: 09 Giugno 2011, 12:48:19 CEST da Nicola_D, Reason: COdice un po piu leggibile... »

Offline racoonlab

  • Utente junior
  • **
  • Post: 98
  • Respect: +1
    • Mostra profilo
  • Dispositivo Android:
    HTC Desire
Re:errore con il cursore
« Risposta #1 il: 09 Giugno 2011, 21:18:07 CEST »
0
Che debba usare "CursorLoader" invece di starManagingCursor?