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

问题描述 投票:-1回答:2

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

这是我的代码:

                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
java android cursor indexoutofboundsexception toast
2个回答
1
投票

首先确保您的游标不为null并且其中包含所需的元素数量。

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


0
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.