异步提取联系人时出现IllegalStateException

时间:2015-03-17 18:26:55

标签: android android-asynctask

当我尝试使用以下代码

在onCreate方法中获取联系人时
Cursor c = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null,null,null,ContactsContract.Contacts.DISPLAY_NAME);
    if (c.getCount()>0) {
        Log.e("Number of contacts :",String.valueOf(c.getCount()));
        while (c.moveToNext()) {
            Log.i("Contacts", c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
        }
    }

它工作。但是当我将代码移动到asyncTask- doInBackground时,它给出了IllegalStateException。这是我的代码

protected ArrayList<String> doInBackground(Void... params) {
        Cursor c = mContext.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null ,null,null,ContactsContract.Contacts.DISPLAY_NAME);
        ArrayList<String> x = new ArrayList<>();
        if (c.getCount() > 0) {
            StringBuilder sb = new StringBuilder();
            while (c.moveToNext()) {
                sb.append(c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
                x.add(sb.toString());
                Log.i("Contacts",sb.toString());
            }
        }
        return x;
    }

这是我的logCat输出

E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:299)
        at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
        at java.util.concurrent.FutureTask.run(FutureTask.java:137)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
        at java.lang.Thread.run(Thread.java:856)
 Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
        at android.database.CursorWindow.nativeGetString(Native Method)
        at android.database.CursorWindow.getString(CursorWindow.java:434)
        at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
        at android.database.CursorWrapper.getString(CursorWrapper.java:114)
        at akshara.xcontacts.adapters.ContactsListAdapter$ContactsLoader.doInBackground(ContactsListAdapter.java:100)
        at akshara.xcontacts.adapters.ContactsListAdapter$ContactsLoader.doInBackground(ContactsListAdapter.java:77)
        at android.os.AsyncTask$2.call(AsyncTask.java:287)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)

at java.util.concurrent.FutureTask.run(FutureTask.java:137) 在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:230) 在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:569) 在java.lang.Thread.run(Thread.java:856)

有谁可以告诉我的代码有什么问题?

1 个答案:

答案 0 :(得分:-1)

确保在执行AsyncTask

之前实例化mContext

如果不起作用请尝试此:

使用 ArrayList<String> x = new ArrayList<String>();

而不是:

ArrayList<String> x = new ArrayList<>();