gpt4 book ai didi

来自 SQLite 的 Android 游标在方法调用时崩溃

转载 作者:搜寻专家 更新时间:2023-10-30 22:15:01 26 4
gpt4 key购买 nike

晚上好!

我正在尝试从我刚刚在我的 android 项目中创建的 SQLite 数据库中读取数据。我创建数据库并毫无问题地输入数据(我认为)。我查询数据库以获取没有任何运行时投诉的数据,并且我得到了从方法调用返回的游标。但是当我尝试使用光标时应用程序崩溃了。 "线程退出并出现未捕获的异常 (group=0x416b72a0)。

这是我要运行的代码。游标的 toString 工作正常。

 TransactionSQLOH helper = new TransactionSQLOH(context, getDbName(yearS, monthS));
SQLiteDatabase db = helper.getReadableDatabase();
String[] columns = {"amount", "label", "year", "month", "day", "categoryID", "type", "ID", "time"};
Cursor cursor = db.query(TransactionSQLOH.TABLE_NAME, columns, null, null, null, null, null);
System.out.println(db);
helper.close();
System.out.println("Transaction_Check3.1");
if (cursor == null){
return null;
}
System.out.println("Transaction_Check3.2");
System.out.println(cursor);
int test = cursor.getCount();
System.out.println("Transaction_Check3.3");
Transaction[] result = new Transaction[10];

我在行中得到了错误

int test = cursor.getCount(); 

帮助将不胜感激:)

编辑,记录:

   02-19 21:08:11.273: I/System.out(21558): Transaction_Check1
02-19 21:08:11.283: I/System.out(21558): Transaction_Check2
02-19 21:08:11.283: I/System.out(21558): SQLiteDatabase: /data/data/com.SverkerSbrg.Spendo/databases/transactions_201301
02-19 21:08:11.283: I/System.out(21558): Transaction_Check3
02-19 21:08:11.288: I/System.out(21558): Transaction_Check3.1
02-19 21:08:11.288: I/System.out(21558): Transaction_Check3.2
02-19 21:08:11.288: I/System.out(21558): android.database.sqlite.SQLiteCursor@421107d8
02-19 21:08:11.288: I/System.out(21558): Transaction_Check3.3
02-19 21:08:11.288: I/System.out(21558): Transaction_Check4
02-19 21:08:11.288: D/AndroidRuntime(21558): Shutting down VM
02-19 21:08:11.288: W/dalvikvm(21558): threadid=1: thread exiting with uncaught exception (group=0x416b72a0)
02-19 21:08:11.328: E/AndroidRuntime(21558): FATAL EXCEPTION: main
02-19 21:08:11.328: E/AndroidRuntime(21558): java.lang.IllegalStateException: Could not execute method of the activity
02-19 21:08:11.328: E/AndroidRuntime(21558): at android.view.View$1.onClick(View.java:3704)
02-19 21:08:11.328: E/AndroidRuntime(21558): at android.view.View.performClick(View.java:4232)
02-19 21:08:11.328: E/AndroidRuntime(21558): at android.view.View$PerformClick.run(View.java:17298)
02-19 21:08:11.328: E/AndroidRuntime(21558): at android.os.Handler.handleCallback(Handler.java:615)
02-19 21:08:11.328: E/AndroidRuntime(21558): at android.os.Handler.dispatchMessage(Handler.java:92)
02-19 21:08:11.328: E/AndroidRuntime(21558): at android.os.Looper.loop(Looper.java:137)
02-19 21:08:11.328: E/AndroidRuntime(21558): at android.app.ActivityThread.main(ActivityThread.java:4921)
02-19 21:08:11.328: E/AndroidRuntime(21558): at java.lang.reflect.Method.invokeNative(Native Method)
02-19 21:08:11.328: E/AndroidRuntime(21558): at java.lang.reflect.Method.invoke(Method.java:511)
02-19 21:08:11.328: E/AndroidRuntime(21558): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
02-19 21:08:11.328: E/AndroidRuntime(21558): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
02-19 21:08:11.328: E/AndroidRuntime(21558): at dalvik.system.NativeStart.main(Native Method)
02-19 21:08:11.328: E/AndroidRuntime(21558): Caused by: java.lang.reflect.InvocationTargetException
02-19 21:08:11.328: E/AndroidRuntime(21558): at java.lang.reflect.Method.invokeNative(Native Method)
02-19 21:08:11.328: E/AndroidRuntime(21558): at java.lang.reflect.Method.invoke(Method.java:511)
02-19 21:08:11.328: E/AndroidRuntime(21558): at android.view.View$1.onClick(View.java:3699)
02-19 21:08:11.328: E/AndroidRuntime(21558): ... 11 more
02-19 21:08:11.328: E/AndroidRuntime(21558): Caused by: java.lang.IllegalStateException: Cannot perform this operation because the connection pool has been closed.
02-19 21:08:11.328: E/AndroidRuntime(21558): at android.database.sqlite.SQLiteConnectionPool.throwIfClosedLocked(SQLiteConnectionPool.java:972)
02-19 21:08:11.328: E/AndroidRuntime(21558): at android.database.sqlite.SQLiteConnectionPool.waitForConnection(SQLiteConnectionPool.java:609)
02-19 21:08:11.328: E/AndroidRuntime(21558): at android.database.sqlite.SQLiteConnectionPool.acquireConnection(SQLiteConnectionPool.java:349)
02-19 21:08:11.328: E/AndroidRuntime(21558): at android.database.sqlite.SQLiteSession.acquireConnection(SQLiteSession.java:894)
02-19 21:08:11.328: E/AndroidRuntime(21558): at android.database.sqlite.SQLiteSession.executeForCursorWindow(SQLiteSession.java:834)
02-19 21:08:11.328: E/AndroidRuntime(21558): at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:62)
02-19 21:08:11.328: E/AndroidRuntime(21558): at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:143)
02-19 21:08:11.328: E/AndroidRuntime(21558): at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:133)
02-19 21:08:11.328: E/AndroidRuntime(21558): at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:196)
02-19 21:08:11.328: E/AndroidRuntime(21558): at android.database.AbstractCursor.moveToFirst(AbstractCursor.java:236)
02-19 21:08:11.328: E/AndroidRuntime(21558): at com.SverkerSbrg.Spendo.Datastructures.Transaction.getMonthTransaciton(Transaction.java:88)
02-19 21:08:11.328: E/AndroidRuntime(21558): at com.SverkerSbrg.Spendo.MainActivity.btn2_click(MainActivity.java:43)
02-19 21:08:11.328: E/AndroidRuntime(21558): ... 14 more

最佳答案

移动:

helper.close();

之后:

int test = cursor.getCount();

您收到 IllegalStateException 异常,因为您在访问数据库的 SQLiteOpenHelper 关闭后尝试使用 Cursor。这会导致错误。

关于来自 SQLite 的 Android 游标在方法调用时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14966694/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com