我在我的 sqlite 助手类上有这个:
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;
}
我在 Activity A 上使用它(在 onCreate 内):
db = new repositorio(Dados.this);
db.open();
produto = db.listaProdutoDeUmaProposta(PROPOSTA);
if ("XXX".equals(produto)) {
//code code code
}
db.close()
我在活动 A 上使用它,如果转到活动 B 并按下后退按钮,应用程序强制关闭,它说光标已经关闭。我该如何解决这个问题?
错误
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
简历:
@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();
}
我从 A 调用活动 B,例如:
I = new Intent("ace.cice.AconselhamentoCliente");
EXTRAS = new Bundle();
EXTRAS.putString("proposta", PROPOSTA);
I.putExtras(EXTRAS);
startActivityForResult(I, request_Code);