java.lang.IllegalStateException: 无法将光标移动到位置 n

java.lang.IllegalStateException: couldn't move cursor to position n

我想导入联系人并将其显示在 ListView 中。 这是我的光标加载器

        ContentResolver cr = getActivity().getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");


        mAdapter = new ContactsAdapter(getActivity(), cur, true, null);

        setListAdapter(mAdapter);

我的适配器是

for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
        // do what you need with the cursor here
        String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        Log.e("name", name + "\n" + cursor.getCount());
        names.add(name);

    }
getIndexList();

这是我的绑定视图

@Override
    public void bindView(View view, Context context, Cursor cursor) {
        // Gets handles to individual view resources
        final ViewHolder holder = (ViewHolder) view.getTag();

        String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

    String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));


for (phoneCursor.moveToFirst(); !phoneCursor.isAfterLast(); phoneCursor.moveToNext()) {
        // do what you need with the cursor here

        String phoneNum = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        Log.e("phoneNum", phoneNum);
        mCheckBox.setTag(phoneNum);
        if (selectedNums.contains(phoneNum)) {
            mCheckBox.setChecked(true);
        } else {
            mCheckBox.setChecked(false);
        }
    }

我的代码在 ListView 结束时崩溃。如果有 2 个联系人我收到错误 java.lang.IllegalStateException: 无法将光标移动到位置 2 如果我有 n 个联系人,情况也是如此。 我看过 this question 但在那里找不到任何答案。请帮助

这是日志

------------- beginning of crash
04-19 14:56:41.583 28188-28188/in.nuventure.com.wedding E/AndroidRuntime: FATAL EXCEPTION: main
Process: in.nuventure.com.wedding, PID: 28188
java.lang.IllegalStateException: couldn't move cursor to position 2
      at android.support.v4.widget.CursorAdapter.getView(CursorAdapter.java:248)
      at android.widget.AbsListView.obtainView(AbsListView.java:2347)
      at android.widget.ListView.makeAndAddView(ListView.java:1864)
      at android.widget.ListView.fillDown(ListView.java:698)
      at android.widget.ListView.fillFromTop(ListView.java:759)
      at android.widget.ListView.layoutChildren(ListView.java:1659)
      at android.widget.AbsListView.onLayout(AbsListView.java:2151)
      at android.view.View.layout(View.java:15671)
      at android.view.ViewGroup.layout(ViewGroup.java:5038)
      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
      at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
      at android.view.View.layout(View.java:15671)
      at android.view.ViewGroup.layout(ViewGroup.java:5038)
      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
      at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
      at android.view.View.layout(View.java:15671)
      at android.view.ViewGroup.layout(ViewGroup.java:5038)
      at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
      at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
      at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
      at android.view.View.layout(View.java:15671)
      at android.view.ViewGroup.layout(ViewGroup.java:5038)
      at android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1037)
      at android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:747)
      at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:761)
      at android.view.View.layout(View.java:15671)
      at android.view.ViewGroup.layout(ViewGroup.java:5038)
      at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1043)
      at android.view.View.layout(View.java:15671)
      at android.view.ViewGroup.layout(ViewGroup.java:5038)
      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
      at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
      at android.view.View.layout(View.java:15671)
      at android.view.ViewGroup.layout(ViewGroup.java:5038)
      at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
      at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
      at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
      at android.view.View.layout(View.java:15671)
      at android.view.ViewGroup.layout(ViewGroup.java:5038)
      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
      at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
      at android.view.View.layout(View.java:15671)
      at android.view.ViewGroup.layout(ViewGroup.java:5038)
      at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
      at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
      at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
      at android.view.View.layout(View.java:15671)
      at android.view.ViewGroup.layout(ViewGroup.java:5038)
      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
      at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
      at android.view.View.layout(View.java:15671)
      at android.view.ViewGroup.layout(ViewGroup.java:5038)
      at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2086)
      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1843)
      at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
      at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
      at android.view.Choreographer.doCallbacks(Choreographer.java:580)
      at android.view.Choreographer.doFrame(Choreographer.java:550)
      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
      at android.os.Handler.handleCallback(Handler.java:739)
      at android.os.Handler.dispatchMessage(Handler.java:95)
      at android.os.Looper.loop(Looper.java:135)
      at android.app.Acti

04-19 14:56:47.361 28188-28200/in.nuventure.com.wedding W/CursorWrapperInner: Cursor finalized without prior close()

我找不到显示的错误行。这让我很困惑

只需使用

cursor.moveToFirst();
while (cursor.moveToNext()){
    //do something
}

在你的 for 循环中,你正在比较光标是否不是 afterlast,也就是说,如果它是最后一个,它仍然会执行不存在的 moveToNext

或者,如果您想坚持使用 for 循环,只需替换

!cursor.isAfterLast()

!cursor.isLast()

感谢您的帮助。此问题与 getCount() 有关。我以前这样做过,后来没注意到。

@Override
    public int getCount() {
        return names.size();
    }

我删除了它,它工作正常。感谢您的帮助