0

我收到这个错误。

09-05 16:17:27.460: E/CursorWindow(29553): Failed to read row 0, column -1 from a CursorWindow which has 1 rows, 8 columns.
09-05 16:17:27.465: E/AndroidRuntime(29553): FATAL EXCEPTION: main
09-05 16:17:27.465: E/AndroidRuntime(29553): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nesv.landstar/com.nesv.landstar.LandstarPage}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.

我不知道我的代码做错了什么。这里是:

Cursor c = null;

        try
        {
            c = landstarDB.rawQuery("SELECT * FROM DriverShipment", null);
        }catch (Exception e) {
            Log.w("Error selecting table", "Error selecting table");
        }

 if (c != null && c.moveToFirst()) {
         c.moveToFirst();
         do {
             Log.i("cctid:", c.getString(c.getColumnIndex("cctid")));
             if(c.getString(c.getColumnIndex("cctid")) == cctid)
             {
                 isRecorded = true;
                 shipmentId = c.getString(c.getColumnIndex("cctid"));
                 origin = c.getString(c.getColumnIndex("origin"));
                 destination = c.getString(c.getColumnIndex("destination"));
                 protectTime = c.getString(c.getColumnIndex("protect_time"));
                 readyTime = c.getString(c.getColumnIndex("ready_time"));
                 etat = c.getString(c.getColumnIndex("eta"));

                 if(c.getString(c.getColumnIndex("isAccepted")) == "1")
                 {
                     isAccepted = true;
                 }
             }
         }while(c.moveToNext());
        }
            c.close();

有任何想法吗?谢谢!

4

3 回答 3

0

column -1告诉你没有 column cctid,并getColumnIndex返回 -1。

c.getString(c.getColumnIndex("cctid")) == cctid

不管用。equals使用该方法比较字符串。

我建议您发布您的表创建查询,以便我们了解为什么该列不存在。

于 2013-09-05T08:31:57.640 回答
0

改变你的循环如下:

if (c.getCount > 0) {
     c.moveToFirst();
     do {
         Log.i("cctid:", c.getString(c.getColumnIndex("cctid")));
         if(c.getString(c.getColumnIndex("cctid")) == cctid)
         {
             isRecorded = true;
             shipmentId = c.getString(c.getColumnIndex("cctid"));
             origin = c.getString(c.getColumnIndex("origin"));
             destination = c.getString(c.getColumnIndex("destination"));
             protectTime = c.getString(c.getColumnIndex("protect_time"));
             readyTime = c.getString(c.getColumnIndex("ready_time"));
             etat = c.getString(c.getColumnIndex("eta"));

             if(c.getString(c.getColumnIndex("isAccepted")) == "1")
             {
                 isAccepted = true;
             }
         }
     }while(c.moveToNext());
    }
于 2013-09-05T08:40:29.740 回答
0

你打 c.moveToFirst(); 了两次电话

if (c != null && c.moveToFirst()) {
         c.moveToFirst(); // Remove this one
于 2013-09-05T08:25:09.893 回答