为什么我的代码会出现这样的错误? CursorIndexOutOfBoundsException 异常

Why am I having such an error to my code? CursorIndexOutOfBoundsException

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

这是我的代码:

                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

也许你应该先把它转换成一个字符串,使用 String userIdString = userid.toString; 如果可能的话。

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

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

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