Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我的代码会出现这样的错误?CursorIndexOutOfBoundsException_Java_Android_Cursor_Indexoutofboundsexception_Toast - Fatal编程技术网

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

Java 为什么我的代码会出现这样的错误?CursorIndexOutOfBoundsException,java,android,cursor,indexoutofboundsexception,toast,Java,Android,Cursor,Indexoutofboundsexception,Toast,有没有人能找到我代码中的错误?我已经尝试了很多东西,但总是以同样的错误告终 这是我的代码: String signeedep = spn.getSelectedItem().toString(); int a = spn1.getSelectedItemPosition(); Cursor userid = db.getallsigneenumber(); u

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

这是我的代码:

                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)返回第二行,因为它是零索引的

调用getString(1)你想做什么?我想得到第1列中的值,它是id。@Justine你的表包含两列还是单列?我加入了两个表,它们总共有7列。也许这可以帮助你:谢谢!我在int变量上加了-1,结果成功了。