0

在以下程序代码CursorIndexOutofBoundexception中,运行应用程序时,logcat 中会显示 a。请给我一个解决方案。

public String getContact(String datevalue,String Userselectvalue) {
        String selection = classdbOpenHelper.KEY_DESC + " = 'Userselectvalue'"  
                +" AND " + classdbOpenHelper.KEY_DATE + " = 'datevalue'";
        SQLiteDatabase db = this.getReadableDatabase(); 
        Cursor cursor = db.query(classdbOpenHelper.DATABASE_TABLE, 
                new String[] {classdbOpenHelper.KEY_EVENT }, selection, 
                null, null, null, null);
             String  place = cursor.getString( );
        return place;
    }

日志猫:

08-05 11:21:13.327: E/AndroidRuntime(827): FATAL EXCEPTION: main
08-05 11:21:13.327: E/AndroidRuntime(827): android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0
08-05 11:21:13.327: E/AndroidRuntime(827):  at android.database.AbstractCursor.checkPosition(AbstractCursor.java:424)
08-05 11:21:13.327: E/AndroidRuntime(827):  at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
08-05 11:21:13.327: E/AndroidRuntime(827):  at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
08-05 11:21:13.327: E/AndroidRuntime(827):  at example.showevent1.classdbOpenHelper.getContact(classdbOpenHelper.java:59)
08-05 11:21:13.327: E/AndroidRuntime(827):  at example.showevent1.FirstActivity$getclicker.onClick(FirstActivity.java:163)
08-05 11:21:13.327: E/AndroidRuntime(827):  at android.view.View.performClick(View.java:4204)
08-05 11:21:13.327: E/AndroidRuntime(827):  at android.view.View$PerformClick.run(View.java:17355)
08-05 11:21:13.327: E/AndroidRuntime(827):  at android.os.Handler.handleCallback(Handler.java:725)
08-05 11:21:13.327: E/AndroidRuntime(827):  at android.os.Handler.dispatchMessage(Handler.java:92)
08-05 11:21:13.327: E/AndroidRuntime(827):  at android.os.Looper.loop(Looper.java:137)
08-05 11:21:13.327: E/AndroidRuntime(827):  at android.app.ActivityThread.main(ActivityThread.java:5041)
08-05 11:21:13.327: E/AndroidRuntime(827):  at java.lang.reflect.Method.invokeNative(Native Method)
08-05 11:21:13.327: E/AndroidRuntime(827):  at java.lang.reflect.Method.invoke(Method.java:511)
08-05 11:21:13.327: E/AndroidRuntime(827):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-05 11:21:13.327: E/AndroidRuntime(827):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-05 11:21:13.327: E/AndroidRuntime(827):  at dalvik.system.NativeStart.main(Native Method)
4

4 回答 4

1
public String getContact(String datevalue,String Userselectvalue) 
{
    String selection = classdbOpenHelper.KEY_DESC + " = 'Userselectvalue'"  
            +" AND " + classdbOpenHelper.KEY_DATE + " = 'datevalue'";
    SQLiteDatabase db = this.getReadableDatabase(); 
    Cursor cursor = db.query(classdbOpenHelper.DATABASE_TABLE, 
            new String[] {classdbOpenHelper.KEY_EVENT }, selection, 
            null, null, null, null);
    String  place = null;

    if(cursor.moveToFirst())
    {
        place = cursor.getString(0);
    }

    return place;
}

以上应该工作。

0中的表示cursor.getString(0)返回记录的列号(0为第一列)

认为游标是愚蠢的。您必须指定他们将在上面的示例中开始阅读 [ 的位置:第一条记录 ( .moveToFirst()) 以及他们何时跳到下一条记录 ( cursor.moveToNext())

不过,您绝对应该检查查询,LogCat 告诉您游标返回 0 条记录;...with a size of 0

于 2013-08-05T11:54:10.850 回答
1

cursor.getString()你应该放之前cursor.moveToFirst()

其实你应该把

if(cursor.moveToFirst())
 place = cursor.getString(**columnIndex**);
return place;

您的示例的光标为空,因此它返回大小 0,位置为 -1。这就是为什么您应该验证呼叫是否为空moveToFirst

于 2013-08-05T11:58:26.057 回答
0

用这个替换你的代码。

public String getContact(String datevalue,String Userselectvalue) 
     {
       String selection = classdbOpenHelper.KEY_DESC + "=' + Userselectvalue + '"  
            +" AND " + classdbOpenHelper.KEY_DATE + " = ' + datevalue + '";
       SQLiteDatabase db = this.getReadableDatabase(); 
       Cursor cursor = db.query(classdbOpenHelper.DATABASE_TABLE, 
            new String[] {classdbOpenHelper.KEY_EVENT }, selection, 
            null, null, null, null);
       if (cursor .getCount() > 0) {
            cursor .moveToFirst();
            do {
                 String  place = cursor.getString(cursor.getColumnIndex(classdbOpenHelper.KEY_EVENT));
            } while (c.moveToNext());
        }
        c.close();
     return place; 
     }
于 2013-08-05T12:32:25.530 回答
0

尝试使用这个:

 public String getContact(String datevalue,String Userselectvalue) 
 {
   String selection = classdbOpenHelper.KEY_DESC + " = 'Userselectvalue'"  
        +" AND " + classdbOpenHelper.KEY_DATE + " = 'datevalue'";
   SQLiteDatabase db = this.getReadableDatabase(); 
   Cursor cursor = db.query(classdbOpenHelper.DATABASE_TABLE, 
        new String[] {classdbOpenHelper.KEY_EVENT }, selection, 
        null, null, null, null);
   if (cursor .getCount() > 0) {
        cursor .moveToFirst();
        do {
             String  place = cursor.getString(0);
        } while (c.moveToNext());
    }
    c.close();
 return place;  }
于 2013-08-05T11:58:18.503 回答