title: Android Closed Cursor
body: <p>I've this on my sqlite helper class:</p>
  
  <pre><code>public String listaProdutoDeUmaProposta(String proposta) {
  
      String sql = "SELECT produto FROM propostas " +
      " WHERE _id =" + proposta;
      String x = "";
      Cursor c = db.rawQuery(sql, null);
      if (c != null) {
          if (c.moveToFirst()) {
              c.moveToFirst();
              x = c.getString(0); 
          }   
      }
      //c.close();
      return x;
  }
  </code></pre>
  
  <p>I use it like this on Activity A (inside onCreate):</p>
  
  <pre><code>    db = new repositorio(Dados.this);
      db.open();
      produto = db.listaProdutoDeUmaProposta(PROPOSTA);
      if ("XXX".equals(produto)) {
          //code code code
      }
      db.close()
  </code></pre>
  
  <p><strong>I'm using it on Activity A, If go to activity B and press the back button the application force to close and it says that the cursor is already closed. How can I solve this?</strong></p>
  
  <p><strong>ERROR</strong></p>
  
  <pre><code>W/SQLiteCursor(1088): requery() failed database /data/data/ace.cice/databases/combinedxxi (conn# 0) already closed
  W/SQLiteCursor(1088): java.lang.IllegalStateException: database /data/data/ace.cice/databases/combinedxxi (conn# 0) already closed
  W/SQLiteCursor(1088):   at android.database.sqlite.SQLiteDatabase.verifyDbIsOpen(SQLiteDatabase.java:2123)
  W/SQLiteCursor(1088):   at android.database.sqlite.SQLiteDatabase.getDbConnection(SQLiteDatabase.java:2448)
  W/SQLiteCursor(1088):   at android.database.sqlite.SQLiteDatabase.getDatabaseHandle(SQLiteDatabase.java:2429)
  W/SQLiteCursor(1088):   at android.database.sqlite.SQLiteCursor.requery(SQLiteCursor.java:412)
  W/SQLiteCursor(1088):   at android.app.Activity.performRestart(Activity.java:4393)
  W/SQLiteCursor(1088):   at android.app.ActivityThread.performRestartActivity(ActivityThread.java:2652)
  W/SQLiteCursor(1088):   at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:163)
  W/SQLiteCursor(1088):   at android.app.LocalActivityManager.dispatchResume(LocalActivityManager.java:518)
  W/SQLiteCursor(1088):   at android.app.ActivityGroup.onResume(ActivityGroup.java:58)
  W/SQLiteCursor(1088):   at ace.cice.DadosApoliceTabs.onResume(DadosApoliceTabs.java:163)
  W/SQLiteCursor(1088):   at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1153)
  W/SQLiteCursor(1088):   at android.app.Activity.performResume(Activity.java:4428)
  W/SQLiteCursor(1088):   at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2217)
  W/SQLiteCursor(1088):   at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2255)
  W/SQLiteCursor(1088):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1028)
  W/SQLiteCursor(1088):   at android.os.Handler.dispatchMessage(Handler.java:99)
  W/SQLiteCursor(1088):   at android.os.Looper.loop(Looper.java:132)
  W/SQLiteCursor(1088):   at android.app.ActivityThread.main(ActivityThread.java:4025)
  W/SQLiteCursor(1088):   at java.lang.reflect.Method.invokeNative(Native Method)
  W/SQLiteCursor(1088):   at java.lang.reflect.Method.invoke(Method.java:491)
  W/SQLiteCursor(1088):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
  W/SQLiteCursor(1088):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
  W/SQLiteCursor(1088):   at dalvik.system.NativeStart.main(Native Method)
  D/AndroidRuntime(1088): Shutting down VM W/dalvikvm(1088): threadid=1: thread exiting with uncaught exception (group=0x40014760)
  E/AndroidRuntime(1088): FATAL EXCEPTION: main 
  E/AndroidRuntime(1088): java.lang.RuntimeException: Unable to resume activity {ace.cice/ace.cice.DadosApoliceTabs}: java.lang.IllegalStateException: trying to requery an already closed cursor 
  E/AndroidRuntime(1088):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2227) 
  E/AndroidRuntime(1088):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2255) 
  E/AndroidRuntime(1088):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1028) 
  E/AndroidRuntime(1088):     at android.os.Handler.dispatchMessage(Handler.java:99) 
  E/AndroidRuntime(1088):     at android.os.Looper.loop(Looper.java:132) 
  E/AndroidRuntime(1088):     at android.app.ActivityThread.main(ActivityThread.java:4025) 
  E/AndroidRuntime(1088):     at java.lang.reflect.Method.invokeNative(Native Method) 
  E/AndroidRuntime(1088):     at java.lang.reflect.Method.invoke(Method.java:491) 
  E/AndroidRuntime(1088):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) 
  E/AndroidRuntime(1088):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 
  E/AndroidRuntime(1088):     at dalvik.system.NativeStart.main(Native Method) 
  E/AndroidRuntime(1088): Caused by: java.lang.IllegalStateException: trying to requery an already closed cursor 
  E/AndroidRuntime(1088):     at android.app.Activity.performRestart(Activity.java:4394) 
  E/AndroidRuntime(1088):     at android.app.ActivityThread.performRestartActivity(ActivityThread.java:2652) 
  E/AndroidRuntime(1088):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:163) 
  E/AndroidRuntime(1088):     at android.app.LocalActivityManager.dispatchResume(LocalActivityManager.java:518) 
  E/AndroidRuntime(1088):     at android.app.ActivityGroup.onResume(ActivityGroup.java:58) 
  E/AndroidRuntime(1088):     at ace.cice.DadosApoliceTabs.onResume(DadosApoliceTabs.java:163) 
  E/AndroidRuntime(1088):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1153) 
  E/AndroidRuntime(1088):     at android.app.Activity.performResume(Activity.java:4428) 
  E/AndroidRuntime(1088):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2217) 
  E/AndroidRuntime(1088):     ... 10 more
  </code></pre>
  
  <p>OnResume:</p>
  
  <pre><code>@Override
  public void onResume() {
      System.out.println("Dados Apolice Tabs ON RESUME");
      db = new repositorio(Dados.this);
      db.open();
      produto = db.listaProdutoDeUmaProposta(PROPOSTA);
      db.close();
          super.onResume();   
  }
  </code></pre>
  
  <p>I call Activity B from A like:</p>
  
  <pre><code>I = new Intent("ace.cice.AconselhamentoCliente");
  EXTRAS = new Bundle();
  EXTRAS.putString("proposta", PROPOSTA);
  I.putExtras(EXTRAS);
  startActivityForResult(I, request_Code);
  </code></pre>