在 Android 中显示光标数据时出现问题

发布于 2024-11-17 10:32:32 字数 584 浏览 5 评论 0原文

运行我的应用程序时,我的 LogCat 中出现以下错误:

06-28 13:39:57.827: 错误/Android运行时(10490): java.lang.RuntimeException:无法 开始活动 组件信息{random.test/random.test.ResultsActivity}: android.database.CursorIndexOutOfBoundsException: 请求索引-1,大小为2

触发错误的代码:

myCursor.getString(0)

我已经检查了我的 SQL 查询,当我在 SQLite 数据库浏览器中运行它时它工作正常(我已经从模拟器中提取了数据库,没有更改)。它返回 1 列,2 行:

--------
| test |
--------
| asd1 |
--------
| asd2 |
--------

当我在光标上运行 getColumnIndex("test") 时,它返回“0”。所以我在这里完全没有想法,它说现有的专栏不存在?

I get the following error in my LogCat when running my application:

06-28 13:39:57.827:
ERROR/AndroidRuntime(10490):
java.lang.RuntimeException: Unable to
start activity
ComponentInfo{random.test/random.test.ResultsActivity}:
android.database.CursorIndexOutOfBoundsException:
Index -1 requested, with a size of 2

The code that triggers the error:

myCursor.getString(0)

I've checked out my SQL query, it works fine when i run it in SQLite Database Browser (I've pulled the db from my emulator, no alterations). It returns 1 column, with 2 rows:

--------
| test |
--------
| asd1 |
--------
| asd2 |
--------

When I run getColumnIndex("test") on my cursor it returns '0'. So I'm all out of ideas here, it says an existing column doesn't exist?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

那支青花 2024-11-24 10:32:32

您可能需要在光标上调用 moveToFirst() 在您尝试使用它之前。另外, getColumnIndex(String) 返回基于 0 的索引,因此如果返回 0,则它是有效列。 -1 表示该列不存在。

You probably need to call moveToFirst() on the Cursor before you try to use it. Also, getColumnIndex(String) returns a 0 based index, so if it returns 0, then it is a valid column. -1 would suggest that the column doesn't exist.

忆梦 2024-11-24 10:32:32

在尝试访问数据之前,您应该执行 myCursor.moveToFirst() 。

You should do myCursor.moveToFirst() before trying to access data.

俏︾媚 2024-11-24 10:32:32

getColumnIndex("test") 只是让您知道列的索引。它没有说明行数...您可以从具有“n”列的表中获得 0 行。

因此,结果为 0 的 getColumnIndex("test") 只是表示您在位置 0 处有一列。

关于 myCursor.getString(0),错误是因为您没有任何结果。
您是否将光标移动到第一个元素?不要忘记调用 moveToFirst()...

希望这对您有帮助...

The getColumnIndex("test") just let you know the index for the column. It says nothing regarding the number of rows... You can have 0 rows from a table with 'n' columns.

So getColumnIndex("test") with a result of 0 just says you have a column in the position 0.

Regarding the myCursor.getString(0), the error is because you don't have any results.
Did you move the cursor to the first element? Don't forget to call moveToFirst()...

Hope this helps you...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文