为什么我的代码出现了这样的错误? CursorIndexOutOfBoundsException

时间:2022-09-04 08:39:53

Is there anyone who can try to find the mistake in my code? I tried many things already but it always ends up with the same error.

是否有人可以尝试在我的代码中找到错误?我已经尝试了很多东西,但总是会出现同样的错误。

This is my code:

这是我的代码:

                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();

This is my error:

这是我的错误:

FATAL EXCEPTION: main

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

2 个解决方案

#1


1  

First make sure your cursor is not null and it is having required number of element in it..

首先确保你的游标不是null,并且它有所需的元素数量。

remember moveToPosition(1) returns the second row, since it's zero-indexed

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

#2


0  

Probably you should first convert this to a String, using String userIdString = userid.toString; if possible.

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

Then you can use userIdString.substring(1,2);

然后你可以使用userIdString.substring(1,2);

#1


1  

First make sure your cursor is not null and it is having required number of element in it..

首先确保你的游标不是null,并且它有所需的元素数量。

remember moveToPosition(1) returns the second row, since it's zero-indexed

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

#2


0  

Probably you should first convert this to a String, using String userIdString = userid.toString; if possible.

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

Then you can use userIdString.substring(1,2);

然后你可以使用userIdString.substring(1,2);