IllegalStateException:从第0行col -1获取字段槽失败

时间:2011-01-16 03:26:51

标签: java android sqlite cursor

这是我的LogCat:

01-15 22:13:08.277: ERROR/AndroidRuntime(28747): java.lang.IllegalStateException: get field slot from row 0 col -1 failed
01-15 22:13:08.277: ERROR/AndroidRuntime(28747):     at android.database.CursorWindow.getLong_native(Native Method)
01-15 22:13:08.277: ERROR/AndroidRuntime(28747):     at android.database.CursorWindow.getLong(CursorWindow.java:414)
01-15 22:13:08.277: ERROR/AndroidRuntime(28747):     at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:108)
01-15 22:13:08.277: ERROR/AndroidRuntime(28747):     at android.database.CursorWrapper.getLong(CursorWrapper.java:127)
01-15 22:13:08.277: ERROR/AndroidRuntime(28747):     at com.mohit.geo2do.adapters.TasksAdapter.bindView(TasksAdapter.java:60)
01-15 22:13:08.277: ERROR/AndroidRuntime(28747):     at android.widget.CursorAdapter.getView(CursorAdapter.java:186)
01-15 22:13:08.277: ERROR/AndroidRuntime(28747):     at android.widget.AbsListView.obtainView(AbsListView.java:2818)
01-15 22:13:08.277: ERROR/AndroidRuntime(28747):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1218)
01-15 22:13:08.277: ERROR/AndroidRuntime(28747):     at android.widget.ListView.onMeasure(ListView.java:1131)
01-15 22:13:08.277: ERROR/AndroidRuntime(28747):     at android.view.View.measure(View.java:8039)
01-15 22:13:08.277: ERROR/AndroidRuntime(28747):     at android.widget.RelativeLayout.measureChild(RelativeLayout.java:554)
01-15 22:13:08.277: ERROR/AndroidRuntime(28747):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:377)
01-15 22:13:08.277: ERROR/AndroidRuntime(28747):     at android.view.View.measure(View.java:8039)
01-15 22:13:08.277: ERROR/AndroidRuntime(28747):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3025)
01-15 22:13:08.277: ERROR/AndroidRuntime(28747):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
01-15 22:13:08.277: ERROR/AndroidRuntime(28747):     at android.view.View.measure(View.java:8039)
01-15 22:13:08.277: ERROR/AndroidRuntime(28747):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:464)
01-15 22:13:08.277: ERROR/AndroidRuntime(28747):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
01-15 22:13:08.277: ERROR/AndroidRuntime(28747):     at android.view.View.measure(View.java:8039)
01-15 22:13:08.277: ERROR/AndroidRuntime(28747):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3025)
01-15 22:13:08.277: ERROR/AndroidRuntime(28747):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
01-15 22:13:08.277: ERROR/AndroidRuntime(28747):     at android.view.View.measure(View.java:8039)
01-15 22:13:08.277: ERROR/AndroidRuntime(28747):     at android.view.ViewRoot.performTraversals(ViewRoot.java:779)
01-15 22:13:08.277: ERROR/AndroidRuntime(28747):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1658)
01-15 22:13:08.277: ERROR/AndroidRuntime(28747):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-15 22:13:08.277: ERROR/AndroidRuntime(28747):     at android.os.Looper.loop(Looper.java:123)
01-15 22:13:08.277: ERROR/AndroidRuntime(28747):     at android.app.ActivityThread.main(ActivityThread.java:4363)
01-15 22:13:08.277: ERROR/AndroidRuntime(28747):     at java.lang.reflect.Method.invokeNative(Native Method)
01-15 22:13:08.277: ERROR/AndroidRuntime(28747):     at java.lang.reflect.Method.invoke(Method.java:521)
01-15 22:13:08.277: ERROR/AndroidRuntime(28747):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862)
01-15 22:13:08.277: ERROR/AndroidRuntime(28747):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
01-15 22:13:08.277: ERROR/AndroidRuntime(28747):     at dalvik.system.NativeStart.main(Native Method)

LogCat引用这行代码01-15 22:13:08.277: ERROR/AndroidRuntime(28747): at com.mohit.geo2do.adapters.TasksAdapter.bindView(TasksAdapter.java:60)。这是我的任务适配器的bindView方法:

@Override
public void bindView(View view, Context context, Cursor cursor) {
    CheckBox checkbox = (CheckBox)view.findViewById(R.id.completed);
    TextView due_date = (TextView)view.findViewById(R.id.due_date);

    String title = cursor.getString(cursor.getColumnIndex(Tasks.TITLE));
    boolean completed = Util.intToBool(cursor.getInt(cursor.getColumnIndex(Tasks.COMPLETED)));

    SimpleDateFormat format = new SimpleDateFormat("EEEEEE, MMM dd yyyy hh:mm aa");
    long unixTime = Calendar.getInstance().getTimeInMillis();
    if (cursor.getLong(cursor.getColumnIndex(Tasks.DUE_DATE)) > 0) {
        Log.v("TasksAdapter", "Unix time is " + unixTime);
        unixTime = cursor.getLong(cursor.getColumnIndex(Tasks.DUE_DATE));
        Calendar due = Util.timestampToDate(unixTime);

        due_date.setText(format.format(due.getTime()));
    }
    checkbox.setText(title);
    checkbox.setChecked(completed);
    due_date.setText("No Deadline");
}

我已100%确认该列确实存在。可能是什么问题呢?

更新
游标来自bindView方法,所以我自己没有为游标创建查询。我重新创建了数据库,现在我收到了这个错误:

01-16 09:46:39.459: ERROR/AndroidRuntime(32563): java.lang.IllegalArgumentException: column 'due_date' does not exist
01-16 09:46:39.459: ERROR/AndroidRuntime(32563):     at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314)
01-16 09:46:39.459: ERROR/AndroidRuntime(32563):     at android.database.CursorWrapper.getColumnIndexOrThrow(CursorWrapper.java:99)
01-16 09:46:39.459: ERROR/AndroidRuntime(32563):     at com.mohit.geo2do.adapters.TasksAdapter.bindView(TasksAdapter.java:64)
01-16 09:46:39.459: ERROR/AndroidRuntime(32563):     at android.widget.CursorAdapter.getView(CursorAdapter.java:186)
01-16 09:46:39.459: ERROR/AndroidRuntime(32563):     at android.widget.AbsListView.obtainView(AbsListView.java:2818)
01-16 09:46:39.459: ERROR/AndroidRuntime(32563):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1218)
01-16 09:46:39.459: ERROR/AndroidRuntime(32563):     at android.widget.ListView.onMeasure(ListView.java:1131)
01-16 09:46:39.459: ERROR/AndroidRuntime(32563):     at android.view.View.measure(View.java:8039)
01-16 09:46:39.459: ERROR/AndroidRuntime(32563):     at android.widget.RelativeLayout.measureChild(RelativeLayout.java:554)
01-16 09:46:39.459: ERROR/AndroidRuntime(32563):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:377)
01-16 09:46:39.459: ERROR/AndroidRuntime(32563):     at android.view.View.measure(View.java:8039)
01-16 09:46:39.459: ERROR/AndroidRuntime(32563):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3025)
01-16 09:46:39.459: ERROR/AndroidRuntime(32563):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
01-16 09:46:39.459: ERROR/AndroidRuntime(32563):     at android.view.View.measure(View.java:8039)
01-16 09:46:39.459: ERROR/AndroidRuntime(32563):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:464)
01-16 09:46:39.459: ERROR/AndroidRuntime(32563):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
01-16 09:46:39.459: ERROR/AndroidRuntime(32563):     at android.view.View.measure(View.java:8039)
01-16 09:46:39.459: ERROR/AndroidRuntime(32563):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3025)
01-16 09:46:39.459: ERROR/AndroidRuntime(32563):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
01-16 09:46:39.459: ERROR/AndroidRuntime(32563):     at android.view.View.measure(View.java:8039)
01-16 09:46:39.459: ERROR/AndroidRuntime(32563):     at android.view.ViewRoot.performTraversals(ViewRoot.java:779)
01-16 09:46:39.459: ERROR/AndroidRuntime(32563):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1658)
01-16 09:46:39.459: ERROR/AndroidRuntime(32563):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-16 09:46:39.459: ERROR/AndroidRuntime(32563):     at android.os.Looper.loop(Looper.java:123)
01-16 09:46:39.459: ERROR/AndroidRuntime(32563):     at android.app.ActivityThread.main(ActivityThread.java:4363)
01-16 09:46:39.459: ERROR/AndroidRuntime(32563):     at java.lang.reflect.Method.invokeNative(Native Method)
01-16 09:46:39.459: ERROR/AndroidRuntime(32563):     at java.lang.reflect.Method.invoke(Method.java:521)
01-16 09:46:39.459: ERROR/AndroidRuntime(32563):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862)
01-16 09:46:39.459: ERROR/AndroidRuntime(32563):     at  com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
01-16 09:46:39.459: ERROR/AndroidRuntime(32563):     at dalvik.system.NativeStart.main(Native Method)

我已经验证列索引是-1:

01-16 09:46:39.434: VERBOSE/TasksAdapter(32563): The index is -1

且due_date列存在:

01-16 09:46:29.839: VERBOSE/TasksProvider(32563): Creating database...
01-16 09:46:29.839: VERBOSE/TasksProvider(32563): _id
01-16 09:46:29.839: VERBOSE/TasksProvider(32563): title
01-16 09:46:29.839: VERBOSE/TasksProvider(32563): completed
01-16 09:46:29.839: VERBOSE/TasksProvider(32563): due_date
01-16 09:46:29.839: VERBOSE/TasksProvider(32563): notes
01-16 09:46:29.839: VERBOSE/TasksProvider(32563): importance

1 个答案:

答案 0 :(得分:13)

确保生成Cursor的查询包含Tasks.DUE_DATE引用的字段。如果没有,getColumnIndex将返回-1。您还可以使用getColumnIndexOrThrow,它允许您捕获IllegalArgumentException。