【问题标题】:Why am I having such an error to my code? CursorIndexOutOfBoundsException为什么我的代码出现这样的错误? CursorIndexOutOfBoundsException
【发布时间】:2017-12-30 12:10:52
【问题描述】:

有没有人可以尝试找出我的代码中的错误?我已经尝试了很多东西,但总是以同样的错误告终。

这是我的代码:

                String signeedep = spn.getSelectedItem().toString();
                int a = spn1.getSelectedItemPosition();
                Cursor userid = db.getallsigneenumber();

                    userid.moveToPosition(a);
                    Toast.makeText(getApplicationContext(),
                             userid.getString(1) + "//this is where the error points out
                             was assigned to the " + signeedep + ".",Toast.LENGTH_SHORT).show();

                    userid.close();
                /*
                db.open();
                db.insertAssignsignee(signeedep, userid.getString(0));
                db.close();
                */
                    myMethod();

这是我的错误:

FATAL EXCEPTION: main

android.database.CursorIndexOutOfBoundsException: Index 1 requested, with a size of 1

【问题讨论】:

  • 你到底想做什么调用 getString(1) ?
  • 我想获取第 1 列中的值,即 id。
  • @Justine 你的表是包含两列还是单列?
  • 我加入了两个表,它们总共有 7 列。
  • 也许这可以帮助你:stackoverflow.com/questions/2848056/…

标签: java android cursor indexoutofboundsexception toast


【解决方案1】:

首先确保你的光标是不为空,并且其中包含所需数量的元素..

记住 moveToPosition(1) 返回第二行,因为它是零索引的

【讨论】:

  • 谢谢!我在我的 int 变量上添加了 -1,它起作用了。
【解决方案2】:

您可能应该首先将其转换为字符串,使用 String userIdString = userid.toString; 如果可能的话。

那么你可以使用userIdString.substring(1,2);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多