IllegalStateException: Could not move cursor to position x

I have a custom CursorWrapper to provide a filtered ListView. My filter goes through each item, which finds the latest version of each "key" and maps that to instantiateTemplateAdapater()

. When I remove the item (I select it and then call it DeleteFormsTask.execute

) that starts AsyncTask and removes the entry. When I try to delete the file, it returns an error couldn't move cursor to position x

where x is the position of the item to be deleted. There are no errors in the line and I have added my entire error log to it as well.

When I change new SmartCursorAdapter(c)

to SimpleCursorAdapter

, the crash disappears, so it seems to me that the problem is with SmartCursorAdapter

.

Question: How can I fix this problem? I can't even debug it.

SimpleCursorAdapter:

public SimpleCursorAdapter instantiateTemplateAdapter() {
        String sortOrder = FormsColumns.DISPLAY_NAME + " ASC, " + FormsColumns.JR_VERSION + " DESC";
        Cursor c = getContentResolver().query(FormsColumns.CONTENT_URI, null, null, null, sortOrder);
        startManagingCursor(c);
        Log.d("FormManagerActivity/instantiateTemplateAdapter", "Cursor count: " + c.getCount());

        String[] fromColumns = { FormsColumns.DISPLAY_NAME, FormsColumns.DISPLAY_SUBTEXT, FormsColumns.JR_VERSION };
        int[] toViews = { R.id.text1, R.id.text2, R.id.text3 };
        return new VersionHidingCursorAdapter(FormsColumns.JR_VERSION, this,
                R.layout.two_item, new SmartCursorWrapper(c), fromColumns, toViews);
}

      

Code for calling the delete method:

mDeleteFormsTask = new DeleteFormsTask();
mDeleteFormsTask.setContentResolver(getContentResolver());
mDeleteFormsTask.setDeleteListener(this);
mDeleteFormsTask.execute(mSelectedIds.toArray(new Long[mSelectedIds.size()]));

      

OnComplete Listener:

@Override
public void formsDeleteComplete(int deletedForms) {
    toast(deletedForms + " form template(s) has been removed");
    if (mAdapter != null)
        mAdapter.notifyDataSetChanged();
    setProgressBarIndeterminateVisibility(false);
}

      

DeleteFormsTask:
  Public class DeleteFormsTask extends AsyncTask {private static final Line t = "DeleteFormsTask";

    private ContentResolver cr;
    private DeleteFormsListener dl;

    private int successCount = 0;

    @Override
    protected Integer doInBackground(Long... params) {
        int deleted = 0;

        if (params == null ||cr == null || dl == null) {
            return deleted;
        }

        // delete files from database and then from file system
        for (int i = 0; i < params.length; i++) {
            if ( isCancelled() ) {
                break;
            }
            try {
                Uri deleteForm =
                        Uri.withAppendedPath(FormsColumns.CONTENT_URI, params[i].toString());
                Cursor c = cr.query(deleteForm, null, null, null, null);

                String remark = "";
                String location = "";
                //Log.d("DeleteFormsTask/doInBackground", "Start Deletion");
                if (c.moveToFirst()) {
                    //Log.d("DeleteFormsTask/doInBackground","We error before this?");
                    remark += c.getString(c.getColumnIndex(FormsColumns.DISPLAY_NAME)) +
                            "|Version: " + c.getString(c.getColumnIndex(FormsColumns.JR_VERSION));
                    location += c.getString(c.getColumnIndex(FormsColumns.FORM_FILE_PATH));
                }
                int wasDeleted = cr.delete(deleteForm, null, null);
                Log.d("DeleteFormsTask/doInBackground", "After deletion");
                deleted += wasDeleted;

                if (wasDeleted > 0) {
                    ActivityLoggerBeta.getInstance().logDeletedTemplate(location, remark);
                }
                Log.d("DeleteFormsTask/doInBackground", "After final Delete");
            } catch ( Exception ex ) {
                Log.e(t,"Exception during delete of: " + params[i].toString() + " exception: "  + ex.toString());
            }
        } 
        successCount = deleted;
        Log.d("DeleteFormsTask/doInBackground", "Right before return.");
        return deleted;
    }

    @Override
    protected void onPostExecute(Integer result) {
        Log.d("DeleteFormsTask/onPostExecute", "DELETED");
        cr = null;

        if (dl != null) {
            dl.formsDeleteComplete(result);
        }
        super.onPostExecute(result);
    }

    @Override
    protected void onCancelled() {
        cr = null;
        if (dl != null) {
            dl.formsDeleteComplete(successCount);
        }
    }

    public void setDeleteListener(DeleteFormsListener listener) {
        dl = listener;
    }

    public void setContentResolver(ContentResolver resolver){
        cr = resolver;
    }

    public int getDeleteCount() {
        return successCount;
    }
}

      

SmartCursorWrapper:

public class SmartCursorWrapper extends CursorWrapper {
    private int[] index;
    private int count;
    private int pos = 0;

    public SmartCursorWrapper(Cursor cursor) {
        super(cursor);
        //assumption: query must be sorted (ASC form_id, DESC form_version)
        count = super.getCount();
        index = new int[count];
        String prevFormId = "";
        for (int i = 0; i < count; i++){
            super.moveToPosition(i);
            String currFormId = cursor.getString(cursor.getColumnIndex(FormsColumns.JR_FORM_ID));
            if (!prevFormId.equals(currFormId)) {
                index[pos++] = i;
                prevFormId = currFormId;
            }
        }
        count = pos;
        pos = 0;
        super.moveToFirst();
    }

    @Override
    public boolean move(int offset) {
        return moveToPosition(pos + offset);
    }

    @Override
    public boolean moveToNext() {
        return moveToPosition(pos + 1);
    }

    @Override
    public boolean moveToPrevious() {
        return moveToPosition(pos - 1);
    }

    @Override
    public boolean moveToFirst() {
        return moveToPosition(0);
    }

    @Override
    public boolean moveToLast() {
        return moveToPosition(count - 1);
    }

    @Override
    public boolean moveToPosition(int position) {
        if (position >= count || position < 0) return false;
        return super.moveToPosition(index[position]);
    }

    @Override
    public int getCount() {
        return count;
    }

    @Override
    public int getPosition() {
        return pos;
    }

}

      

Error response:

01-02 17:30:23.285: D/DeleteFormsTask/doInBackground(4995): After deletion
01-02 17:30:23.285: D/AndroidRuntime(4995): Shutting down VM
01-02 17:30:23.285: W/dalvikvm(4995): threadid=1: thread exiting with uncaught exception (group=0x417ccda0)
01-02 17:30:23.295: D/DeleteFormsTask/doInBackground(4995): After final Delete
01-02 17:30:23.295: D/DeleteFormsTask/doInBackground(4995): Right before return
01-02 17:30:23.295: E/AndroidRuntime(4995): FATAL EXCEPTION: main
01-02 17:30:23.295: E/AndroidRuntime(4995): Process: org.marcims.collect, PID: 4995
01-02 17:30:23.295: E/AndroidRuntime(4995): java.lang.IllegalStateException: couldn't move cursor to position 6
01-02 17:30:23.295: E/AndroidRuntime(4995):     at android.widget.CursorAdapter.getView(CursorAdapter.java:246)
01-02 17:30:23.295: E/AndroidRuntime(4995):     at android.widget.AbsListView.obtainView(AbsListView.java:2707)
01-02 17:30:23.295: E/AndroidRuntime(4995):     at android.widget.ListView.makeAndAddView(ListView.java:1811)
01-02 17:30:23.295: E/AndroidRuntime(4995):     at android.widget.ListView.fillDown(ListView.java:697)
01-02 17:30:23.295: E/AndroidRuntime(4995):     at android.widget.ListView.fillSpecific(ListView.java:1360)
01-02 17:30:23.295: E/AndroidRuntime(4995):     at android.widget.ListView.layoutChildren(ListView.java:1619)
01-02 17:30:23.295: E/AndroidRuntime(4995):     at android.widget.AbsListView.onLayout(AbsListView.java:2546)
01-02 17:30:23.295: E/AndroidRuntime(4995):     at android.view.View.layout(View.java:15656)
01-02 17:30:23.295: E/AndroidRuntime(4995):     at android.view.ViewGroup.layout(ViewGroup.java:4869)
01-02 17:30:23.295: E/AndroidRuntime(4995):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
01-02 17:30:23.295: E/AndroidRuntime(4995):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
01-02 17:30:23.295: E/AndroidRuntime(4995):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
01-02 17:30:23.295: E/AndroidRuntime(4995):     at android.view.View.layout(View.java:15656)
01-02 17:30:23.295: E/AndroidRuntime(4995):     at android.view.ViewGroup.layout(ViewGroup.java:4869)
01-02 17:30:23.295: E/AndroidRuntime(4995):     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
01-02 17:30:23.295: E/AndroidRuntime(4995):     at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
01-02 17:30:23.295: E/AndroidRuntime(4995):     at android.view.View.layout(View.java:15656)
01-02 17:30:23.295: E/AndroidRuntime(4995):     at android.view.ViewGroup.layout(ViewGroup.java:4869)
01-02 17:30:23.295: E/AndroidRuntime(4995):     at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:429)
01-02 17:30:23.295: E/AndroidRuntime(4995):     at android.view.View.layout(View.java:15656)
01-02 17:30:23.295: E/AndroidRuntime(4995):     at android.view.ViewGroup.layout(ViewGroup.java:4869)
01-02 17:30:23.295: E/AndroidRuntime(4995):     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
01-02 17:30:23.295: E/AndroidRuntime(4995):     at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
01-02 17:30:23.295: E/AndroidRuntime(4995):     at android.view.View.layout(View.java:15656)
01-02 17:30:23.295: E/AndroidRuntime(4995):     at android.view.ViewGroup.layout(ViewGroup.java:4869)
01-02 17:30:23.295: E/AndroidRuntime(4995):     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2247)
01-02 17:30:23.295: E/AndroidRuntime(4995):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1969)
01-02 17:30:23.295: E/AndroidRuntime(4995):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1201)
01-02 17:30:23.295: E/AndroidRuntime(4995):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6404)
01-02 17:30:23.295: E/AndroidRuntime(4995):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
01-02 17:30:23.295: E/AndroidRuntime(4995):     at android.view.Choreographer.doCallbacks(Choreographer.java:603)
01-02 17:30:23.295: E/AndroidRuntime(4995):     at android.view.Choreographer.doFrame(Choreographer.java:573)
01-02 17:30:23.295: E/AndroidRuntime(4995):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
01-02 17:30:23.295: E/AndroidRuntime(4995):     at android.os.Handler.handleCallback(Handler.java:733)
01-02 17:30:23.295: E/AndroidRuntime(4995):     at android.os.Handler.dispatchMessage(Handler.java:95)
01-02 17:30:23.295: E/AndroidRuntime(4995):     at android.os.Looper.loop(Looper.java:157)
01-02 17:30:23.295: E/AndroidRuntime(4995):     at android.app.ActivityThread.main(ActivityThread.java:5335)
01-02 17:30:23.295: E/AndroidRuntime(4995):     at java.lang.reflect.Method.invokeNative(Native Method)
01-02 17:30:23.295: E/AndroidRuntime(4995):     at java.lang.reflect.Method.invoke(Method.java:515)
01-02 17:30:23.295: E/AndroidRuntime(4995):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
01-02 17:30:23.295: E/AndroidRuntime(4995):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
01-02 17:30:23.295: E/AndroidRuntime(4995):     at dalvik.system.NativeStart.main(Native Method)

      

+3


source to share





All Articles